Exception Handling Flashcards
Exception
An object that is generated as a result of an error or an unexpected event. Unless the exception is handled in your code it will crash your program!
Exception handler
A section of code that gracefully responds to exceptions when they are thrown
Exception handling
The process of intercepting and responding to exceptions
Default exception handler
Prints an error message and crashes the program
Exception class hierarchy
An exception is object that is created from a class in the Java API, all of which inherit from the Throwable class. Just below the Throwable class are the Error and Exception classes. Classes that inherit from Error are for exceptions that are thrown when a critical error occurs such as an internal error in the JVM or running out of memory. Your programs should not try to handle these errors!
All of the exceptions that programmers are expected to handle are instances of the classes that inherit from the Exception class. Each exception object has a method named getMessage() that can be used to retrieve the default error message for the exception.
How do you handle an exception?
With a try block and catch statement. A try block is one or more statements that are executed and can potentially throw an exception. You can think of the code in the try block as being “protected” because the application will not halt if the try block throws an exception.
A catch clause begins with the key word catch followed by the code (ExceptionType parameterName) where ExceptionType is the name of an exception class and parameterName is a variable name. If the code in the try block throws an exception of the ExceptionType class then the code in the catch block is executed.
Note that a try block may be accompanied by more than one catch clause as different types of exceptions may be possible!
Finally clause
One or more statements that are always executed after the try block has executed and after any catch blocks have executed if an exception was thrown. That is, it will always be executed. You could use this to close a file, for example.
Stack trace
This is helpful to use when a method calls another method or multiple methods. It is list of all the methods in the call stack (a list of methods that are currently executing) that indicates which one was executing when an exception occurred and all of the methods that were called in order to execute that method.
Multi-catch
The ability to catch multiple types of exceptions with a single catch clause. For example:
catch ( NumberFormatException | IOException | InputMismatchException e)
Checked exceptions
Exceptions that do no inherit from the Error or RuntimeException classes that must be handled in your program. Methods that throw checked exceptions must have a throws clause in their headers listing the exception type if they’re not handled by that method.
Unchecked exceptions
Exceptions that inherit from the Error or RuntimeException classes and should not be handled in your program because the conditions that cause them can rarely be dealt with as such. These do not require a throws clause in method headers.
“throws” key word / throws clause
This is included at the end of a method header and before the specific type/s of exceptions that it may generate. For example:
displayFile(String name) throws FileNotFoundException
This tells the compiler that the method can throw a checked exception that isn’t handled so that it’s passed up the call stack.
Throw statement
You can use a throw statement to throw an exception, one of the standard Java exceptions or an instance of a custom exception class, manually. The general format is:
throw new ExceptionType(MessageString);
Where the MessageString is an optional parameter that can be used to display a custom error message from the exception object’s getMessage method.
For example:
throw new Exception(“Out of fuel”);