Junior — Middle
What constructs in Python are designed for error and exception handling?
sobes.tech AI
Answer from AI
In Python, constructs try, except, else, and finally are used for error handling and exception management.
try— a block where code that might raise exceptions is executed.except— blocks for catching and handling specific exceptions.else— a block that executes if no exceptions occurred.finally— a block that always executes, regardless of whether an exception was raised or not.
Example:
try:
result = 10 / 0
except ZeroDivisionError:
print("Division by zero is not possible")
else:
print("Result:", result)
finally:
print("Finally block executed")