EXCEPTION HANDLING Flashcards

1
Q

What is an exception?

A

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

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

What is the call stack

A

A call stack is an ordered list of methods that had been called to get to
the method where the error occurred

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

What is an exception handler?

A

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

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

Describe The Catch or Specify Requirement

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

What are the three common types of exceptions

A

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

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

Give some Exception Handling and Catching components

A

i. try block
ii. catch block
iii. finally block
iv. try-with-resources statement (introduced in Java SE 7)

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

Give the syntax of a try catch statement

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

Describe Try-with-resources

A

• 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.

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