Junior — Middle
What is the role of the try...except construct in error handling in programming?
sobes.tech AI
Answer from AI
The try...except construct in Python is used for exception handling — errors that may occur during program execution. It allows you to "catch" an error, prevent the program from crashing, and execute alternative code, such as displaying an error message or performing corrective actions.
Example:
try:
result = 10 / 0
except ZeroDivisionError:
print("Division by zero is not possible")
In this example, attempting to divide by zero raises a ZeroDivisionError exception, which is caught by the except block, and the program continues to run without crashing.