WorksheetsPython Exceptions and Exception Handling Worksheet
Total questions: 20
Worksheet time: 10mins
What is the main purpose of Python exceptions?
To detect syntax errors during parsing
To handle errors that occur during program execution in syntactically correct code
To automatically fix runtime bugs
To improve code compilation speed
How does exception handling primarily benefit a program?
It speeds up execution
It prevents crashes and improves user experience
It replaces the need for debugging tools
It only works with syntax errors
Which of the following is NOT listed as a benefit of exception handling in the document?
Prevents application crashes
Improves user experience
Allows graceful degradation
Automatically corrects invalid input
In the basic try-except example, what output is printed when dividing 100 by 4?
Something went wrong! Please try again.
100 divided by your number is: 25.0
ZeroDivisionError: division by zero
Invalid input! Please enter a number.
What happens in a try-except block when dividing 100 by 0 using a general except clause?
The program crashes with a traceback
It prints "Something went wrong! Please try again."
It skips the except block entirely
It raises a SyntaxError
Why is catching specific exceptions (like ValueError) recommended over a general except?
It catches fewer errors
It provides meaningful messages and improves code clarity
It only works for ZeroDivisionError
It makes the code longer without benefits
In the specific exception catching example, what message is displayed if the user enters "abc"?
You can't divide by zero!
That's not a valid number!
100 divided by your number is: 100.0
Calculation successful.
What is the role of the else clause in a try-except-else block?
It runs only if an exception occurs
It executes if no exception occurs in the try block
It always runs, like finally
It handles syntax errors
In the else clause example, what output follows successful input of 4?
Division by zero is not allowed.
Invalid input! Please enter a number.
Calculation successful. Result: 25.0
Program execution completed.
When does the finally clause execute?
Only if no exception occurs
Only if an exception is caught
Regardless of whether an exception occurred
Never, unless explicitly called
In the finally clause example with input 0, which message is always printed last?
Result: infinite
Error: Division by zero is not allowed.
Program execution completed.
Error: Please enter a valid number.
What keyword is used to manually signal an error condition, like invalid age?
try
except
raise
else
In the raising exceptions example, what message is printed for age input 125?
Age cannot be negative
Processing age: 125
Invalid age: Are you sure this age is correct?
Division by zero is not allowed.
How is a custom exception class typically defined in Python?
class MyError: pass
class MyError(Exception): pass
def MyError(): raise ValueError
except MyError: print("Error")
In the custom exceptions example, what exception is raised when withdrawing 150from 100?
ValueError
ZeroDivisionError
InsufficientFundsError with a specific message
SyntaxError
What does exception chaining (using raise ... from ...) primarily help with?
Ignoring the original error
Preserving the original error cause for debugging
Catching multiple exceptions at once
Automatically fixing chained errors
In the exception chaining example, what is printed as the "Original cause" for int("abc")?
Conversion failed
invalid literal for int() with base 10: 'abc'
Division by zero
Age cannot be negative
Which syntax catches multiple exceptions in one block?
except ValueError or ZeroDivisionError:
except (ValueError, ZeroDivisionError):
except ValueError, ZeroDivisionError:
except multiple [ValueError, ZeroDivisionError]:
What are context managers (using with) commonly used for in exception handling?
Raising custom exceptions
Automatically managing resources like files, ensuring cleanup
Only catching syntax errors
Replacing try-except blocks entirely
Which of the following is a best practice for exception handling from the document?
Use empty except blocks for simplicity
Catch specific exceptions instead of general ones
Use exceptions for normal flow control
Hide all errors without documentation
