Exceptions&IO Flashcards
What is an exception handler?
A section of code that gracefully responds to exceptions.
What is a default exception handler?
Deals with unhandled exceptions, prints an error message, crashes the program.
How do you handle an exception?
try
{ (try block statements…)
}
catch (ExceptionType ParameterName)
{ (catch block statements…)
}
How do you handle multiple exceptions?
● The code in the try block may be capable of throwing more than one type of
exception.
● A catch clause needs to be written for each type of exception that could
potentially be thrown.
● The JVM will run the first compatible catch clause found.
● The catch clauses must be listed from most specific to most general.
● For a given thrown exception, only one catch block will be executed. Your logic
should not be spread across multiple catch blocks.
What is a finally block?
A block may be used to specify code that will be
executed last, whether the code in a try block causes an exception or not.
The code in a finally block is always executed, even if an exception is rethrown.
What is a custom exception?
Custom exceptions usually define one or
more constructors to accept a detailed error
message, another Throwable (the root
cause), or both.