Test your expertise on Python's exception handling mechanisms, from advanced try/except usage to custom exception creation and propagation behaviors.
Nested Exception Handling
What will be printed by the following code: try: raise ValueError('error') except TypeError: print('TypeError') except: print('General')?
- General
- error
- ValueError
- TypeError
- No Output
Finally Block Behavior
In Python, which scenario will cause the 'finally' block to not be executed?
- An exception is raised in the corresponding try block
- None of the above
- The except block does not catch the exception
- System is forcibly terminated using os._exit(0)
- There is a return statement in both try and finally blocks
Multiple Exception Handling
Given the code: try: ... except (KeyError, IndexError): ...; which statement is true if a TypeError is raised in the try block?
- The except block will catch all exceptions
- The code will terminate silently
- A SyntaxError will be raised
- The exception will propagate up the call stack
- The except block will handle the exception
Custom Exceptions
Which is the correct way to define a custom exception in Python named MyException?
- class MyException: pass
- class MyException(Exception): pass
- def MyException(): pass
- exception MyException(Exception): pass
- raise MyException()
Exception Propagation
If an exception is raised in a try block and not handled, what is the default behavior in Python?
- It is automatically converted into a warning
- A warning is displayed but execution proceeds
- The program crashes and displays a traceback
- It is silently ignored
- The program continues from the next line
Raise Statement Behavior
Which statement correctly raises a built-in exception with a message?
- raise(ValueError: 'Invalid input')
- raise ValueError('Invalid input')
- except ValueError('Invalid input')
- throw ValueError('Invalid input')
- raise ValueError, 'Invalid input'
Re-raising Exceptions
What is the effect of calling just 'raise' inside an except block with no exception object specified?
- It clears the exception and resumes execution
- It re-raises the currently handled exception
- It does nothing
- It raises a SyntaxError
- It raises a NameError
Exception Hierarchies
Which of the following is NOT a direct built-in subclass of Exception in Python?
- GeneratorExit
- TypeError
- OSError
- ValueError
- KeyError
Else with Try/Except
What is the purpose of the 'else' clause in a try/except/else construct?
- It runs only if an exception is raised
- It always runs after except
- It skips the finally block
- It must immediately follow finally
- It runs only if the try block does not raise any exception
Exception Context
Given nested exceptions, how can you access the original exception from a newly raised exception using 'raise ... from ...'?
- By using sys.exit()
- By accessing the __cause__ attribute
- By catching both exceptions in a tuple
- By using the args attribute
- By checking the traceback object
Catching All Exceptions
Which except clause would catch every exception except system-exiting ones like SystemExit and KeyboardInterrupt?
- except BaseException:
- except Exception:
- except AllExceptions:
- except StandardError:
- except:
Suppressing Exceptions
Which module provides a context manager for suppressing specified exceptions temporarily in Python 3.4+?
- functools
- exceptions
- contextlib
- os
- sys
Assertions vs. Exceptions
What type of exception does the 'assert' statement raise if its expression evaluates to False?
- LogicError
- AssertionError
- ValueError
- TypeError
- RuntimeError
Uncaught Exception Logging
Which standard library module is typically used to log uncaught exceptions in Python?
- traceback
- errorlog
- debug
- logging
- warning
Exception Arguments
How do you access the arguments passed when raising a custom exception instance in Python?
- They are not accessible
- Directly as exception.message
- Through the exception's __str__ method only
- By reading the exception object's args attribute
- By calling getValue() on the exception object