Week 13 - Exception event FPL Flashcards
What are exceptions?
Exceptions are objects of classes
What are the two predefined subclasses of the Throwable class?
Error and Exception.
When are Error exceptions thrown?
Error exceptions are thrown by the runtime system for events such as heap overflow and are never handled by user programs.
What are user-defined exceptions typically subclasses of?
User-defined exceptions are typically subclasses of the Exception class.
What happens if no handler is found in the try construct?
If no handler is found in the try construct, the search continues in the nearest enclosing try construct. If no handler is found in the method, the exception is propagated to the method’s caller, and if no handler is found all the way to main, the program is terminated.
What are unchecked exceptions?
Unchecked exceptions are those of class Error and RuntimeException and all their descendants. They are not checked by the compiler.
How is binding exceptions to handlers simpler compared to C++?
Binding an exception to a handler is simpler because an exception is bound to the first handler with a parameter of the same class as the thrown object or an ancestor of it.
How are exceptions thrown?
Exceptions are thrown using the throw statement, often combined with the new operator to create the exception object.
What is the purpose of the finally clause?
The finally clause is used to specify code that is executed regardless of whether an exception is thrown in the try construct.
What must be done with checked exceptions?
Checked exceptions that may be thrown by a method must be either listed in the method’s throws clause or handled within the method.
Name two predefined subclasses of the Exception class.
IOException and RuntimeException are two predefined subclasses of the Exception class.
Can a method declare more exceptions in its throws clause than the method it overrides?
No
What are the three alternatives for dealing with an exception listed in a called method’s throws clause?
1) Catch and handle the exception
2) Catch the exception and throw an exception listed in its own throws clause
3) Declare the exception in its throws clause and do not handle it.
What happens when an assert statement evaluates to false?
When an assert statement evaluates to false, an AssertionError exception is thrown.
What is a key advantage of the throws clause compared to C++?
The throws clause provides more information to the programmer by specifying which exceptions a method can throw, unlike the throw clause in C++ which says little.
What is the purpose of the else block in the try-except construct?
The else block in the try-except construct is executed if no exceptions are raised in the try block.
What does the assert statement do?
The assert statement tests a Boolean expression and raises an AssertionError with an optional message if the expression evaluates to false.
How are exception handlers written?
Exception handlers are written using the try-except structure, where each except block specifies the type of exception it handles.
How are exceptions raised in another language?
Exceptions are raised using the raise statement, which can include an error message.
Where are exception handlers placed in another language?
Exception handlers are placed at the end of a begin block and are introduced by the rescue keyword.