Monday, September 14, 2026

Python Exception Handling

 Python Exception Handling: try, except, else & finally


1. What is the output? try: print(10 / 0) except ZeroDivisionError: print("Error")

   A. 0

   B. Nothing

   C. Error

   D. 10


2. What is the output? try: x = 10 except: print("Error") else: print("Success")

   A. Error

   B. 10

   C. Success

   D. Nothing


3. What is the output? try: print(5 / 0) except ZeroDivisionError: print("Caught") finally: print("Done")

   A. Caught

   B. Done

   C. Error\nDone

   D. Caught\nDone


4. What is the output? try: print("A") except: print("B") finally: print("C")

   A. C\nA

   B. A

   C. A\nC

   D. B\nC


5. Which block is designed to execute whether or not an exception occurs? try: # risky operation except Exception: # handle error else: # no error finally: # cleanup

   A. except

   B. try

   C. finally

   D. else


6. What is the output? try: int("abc") except ValueError: print("Invalid") else: print("Valid") finally: print("Finished")

   A. Invalid

   B. Invalid\nFinished

   C. Valid\nFinished

   D. Finished\nInvalid


7. What happens to the else block when the try block raises an exception that is handled by except?

   A. It executes before except

   B. It is skipped

   C. It always executes

   D. It executes after finally


8. What is the output? try: x = 10 print(x) except Exception: print("Error") finally: print("Cleanup")

   A. Error\nCleanup

   B. 10\nCleanup

   C. Cleanup\n10

   D. 10


9. What is the main purpose of the else block in Python exception handling?

   A. Run code only when try fails

   B. Run code only when try succeeds

   C. Always clean up resources

   D. Catch every exception


10. What is the output? try: print("Start") raise Exception("Oops") except Exception: print("Handled") finally: print("End")

   A. Handled\nEnd

   B. Start\nEnd

   C. Start\nHandled

   D. Start\nHandled\nEnd

1 comment:

perlknowledge said...

What is the output?

try: print(10 / 0) except ZeroDivisionError: print("Error")

The division raises ZeroDivisionError, and the matching except block prints Error.

What is the output?

try: x = 10 except: print("Error") else: print("Success")

Because the try block completes normally, the else block executes and prints Success.

What is the output?

try: print(5 / 0) except ZeroDivisionError: print("Caught") finally: print("Done")

The ZeroDivisionError is handled by except, then finally executes regardless of the exception.

What is the output?

try: print("A") except: print("B") finally: print("C")

The try block prints A without an exception, then finally prints C.

Which block is designed to execute whether or not an exception occurs?

try:

# risky operation

except Exception:

# handle error

else:

# no error

finally:

# cleanup

The finally block is intended for code that should execute whether an exception occurs or not.

What is the output?

try: int("abc") except ValueError: print("Invalid") else: print("Valid") finally: print("Finished")

ValueError is handled by except, so Invalid is printed, followed by finally printing Finished.

What happens to the else block when the try block raises an exception that is handled by except?

When the try block raises a handled exception, execution goes to except and skips else.

What is the output?

try: x = 10 print(x) except Exception: print("Error") finally: print("Cleanup")

The assignment and print succeed, so 10 is printed, followed by finally printing Cleanup.

What is the main purpose of the else block in Python exception handling?

The else block runs when the try block completes without raising an exception.

What is the output?

try: print("Start") raise Exception("Oops") except Exception: print("Handled") finally: print("End")

The statements execute in order: Start, the exception is handled, then finally runs.