Exception Handling Flashcards
What is an exception?
run-time error
an abnormal condition that arises at runtime.
How does exception handling work?
Use the words try, catch, throw, finally
Use a try block to monitor for exceptions.
If exception occurs, it is thrown.
The code can catch the exception and handle it in some way.
finally specifies code that will run after the try block
How do you define the exception to the thrown from a method?
throws
static void throwOne() throws IllegalAccessException {
throw new IllegalAccessException(“demo”)
What is the superclass of all exceptions?
Throwable
What are the two subclasses of Throwable?
Exception - normal, in user programs, used to create own exceptions
Error - catastrophic, usually having to do with runtime environment
T/F: once an exception is thrown, it must be caught.
True.
If not specified, default handler will handle it
Do you need to include catch in a try statement?
no
Whats the goal of a catch clause?
resolve the error condition then continue on as if the error never occured.
in contrast to without it, the program would stop.
Can you have multiple catch clauses?
yes. The execute in order, then stop when one is hit.
For multiple catch clauses, Why do exception subclasses must come before their superclass?
Because it’ll produce an “unreachable code” error in that the exception will never be able to get to the subclass because it’ll always be caught with the superclass before
what is “throw” used for?
Can determine the exception to throw instead of the default provided by java
Why does a method need to define the exceptions it can throw?
so that the method caller can handle it.
Only need define the ones outside of type Error or RuntimeException which are included in java
Will a finally block execute even if an exception is not found?
yes
T/F
think of try, throw, and catch as clean ways to handle errors and unusual boundary conditions in your program’s logic
ya
in Exception class, what method do you call to get the description of the error?
except.getMessage()