Exercise: Exception Handling
Questions for: Exception Handling
What does the
try and except blocks in Python exception handling allow you to do?
A:
Define custom exception classes
B:
Raise exceptions intentionally
C:
Handle and catch exceptions
D:
Terminate the program
Answer: C
The
try block is used to enclose code that might raise an exception, and the except block is used to handle and catch the raised exceptions.
What is the purpose of the
pass statement in exception handling?
A:
To re-raise an exception
B:
To ignore an exception and continue with the program execution
C:
To define custom exception classes
D:
To print the exception message
Answer: B
The
pass statement is used as a placeholder to ignore an exception and continue with the program execution when no action is required.Discuss About this Question.
In Python, what is the purpose of the
else block in exception handling?
A:
It is executed if an exception occurs
B:
It is used to define custom exception classes
C:
It is executed if no exception occurs in the try block
D:
It is used to raise exceptions
Answer: C
The
else block is executed only if no exceptions are raised in the associated "try" block. It is used to specify code that should run when there are no exceptions.Discuss About this Question.
Which of the following is a built-in exception in Python for handling errors related to accessing a key that does not exist in a dictionary?
A:
KeyError
B:
ValueNotFoundError
C:
NoSuchElementError
D:
DictionaryError
Answer: A
A
KeyError is raised when attempting to access a key in a dictionary that does not exist.Discuss About this Question.
Consider the following Python code:
try:
result = 10 / 0
except ZeroDivisionError:
result = "Error: Division by zero"
What will be the value of "result" after the execution of this code?
A:
10
B:
0
C:
"Error: Division by zero"
D:
None
Answer: C
The code attempts to divide by zero, which raises a
ZeroDivisionError. The exception is caught, and "result" is assigned the string "Error: Division by zero."Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.