Exception Handling Flashcards

1
Q

Exception

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Exception handler

A

A section of code that gracefully responds to exceptions when they are thrown

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Exception handling

A

The process of intercepting and responding to exceptions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Default exception handler

A

Prints an error message and crashes the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Exception class hierarchy

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you handle an exception?

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Finally clause

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Stack trace

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Multi-catch

A

The ability to catch multiple types of exceptions with a single catch clause. For example:

catch ( NumberFormatException | IOException | InputMismatchException e)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Checked exceptions

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Unchecked exceptions

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

“throws” key word / throws clause

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Throw statement

A

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”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly