EXCEPTION HANDLING Flashcards
What is an exception?
An exception is an event, which occurs during the
execution of a program, that disrupts the normal flow of
the program’s instructions.
• It could also be defined as an abnormal condition that
occurs at run time
What is the call stack
A call stack is an ordered list of methods that had been called to get to
the method where the error occurred
What is an exception handler?
This is a method that contains a block of code that can handle an exception, usually searched through in the call stack.
• The search begins with the error method, proceeding through the call stack in reverse order in which the methods were called.
• The exception handler chosen catches the exception.
• If not, runtime system usually terminates
Describe The Catch or Specify Requirement
- The catch/specify requirement requires that any code that might throw an exception must be enclosed by either of the following:
- A try statement
- A method containing a throws clause listing an exception
- Only checked exceptions are subject to this requirement, where non-compliant code will fail to compile.
What are the three common types of exceptions
I. Checked exceptions: exceptional conditions that a well-written application should anticipate and recover from, e.g. java.io.FileNotFoundException
II. Error: exceptional conditions that are external to an application. Usually difficult for an application to deal with as it must be dealt with externally, e.g. resource allocation for optimal operation. They are thus mainly caught for notification purposes.
III. Runtime exception: exceptional conditions internal to the application, usually because of a programmer mistake. They occur after the programmer runs a particular code, and thus can be rectified through exhaustive testing, e.g. NullPointerException
Give some Exception Handling and Catching components
i. try block
ii. catch block
iii. finally block
iv. try-with-resources statement (introduced in Java SE 7)
Give the syntax of a try catch statement
try { } catch (ExceptionType name) { } catch (ExceptionType name) { } finally { } • The try block is where an exception occurs. • The catch block is an exception handler that handles the type of exception indicated by its argument. The argument type, ExceptionType, declares the type of exception that the handler can handle and must be the name of a class that inherits from the Throwable class (two main subclasses: Error and Exception). • The catch block contains code that is executed if and when the exception handler is invoked • The finally block executes when the try block exits. It may also execute in case an unexpected exception occurs.
Describe Try-with-resources
• A try-with-resources statement (introduced since JDK 7), is a try
statement that declares one or more resources.
• A resource is an object or file that is being utilized in the event of an
execution.
• The try-with-resources statement is used to release a resource after the
program is finished with it.
try(// open resources here){
// use resources
} catch (FileNotFoundException e) {
// exception handling
}
// resources are closed as soon as try-catch block is executed.