How do you catch one specific exception in Python?
Experience Level: Junior
Tags: Python
Answer
Use try/except block. In the except block define the type of exception that you want to catch.
Example:
def my_method():
try:
int("This will cause exception to be raised")
except Exception:
print("Something went wrong.")
Related Python job interview questions
What is Python PIP?
Python JuniorHow do you catch multiple different exceptions in Python and handle each of them differently?
Python JuniorWhat does **kwargs mean in Python?
Python JuniorWhat does *args mean in Python?
Python JuniorHow do you reraise an exception in Python?
Python Junior