Java Exceptions  «Prev  Next»

Lesson 2 Understanding exceptions
ObjectiveGet acquainted with Exceptions and their Significance in Java Programming.

Why Exceptions are important in Java

If a computer runs out of memory or a network connection fails, an error is generated. But in this case, your code is not to blame.
These types of exceptional situations are known in Java programming as exceptions. An exception is a problem that prevents the continued execution of a program for one reason or another. If a network connection is lost, it is pointless to attempt to read from it. Java includes special constructs to detect exceptions and deal with them. In the case of the network connection problem, you might try to reestablish the connection in response to the exception. Note: Exception handling is built into the Java programming language. This characteristic makes Java unique from some programming languages such as C. Exceptions form a critical part of Java's approach to handling problems that are otherwise difficult to handle through traditional programming strategies. An exception is a subclass of Throwable.
Exceptions can be divided into three groups: errors, runtime exceptions, and normal exceptions.

Three Advantages of Java Exceptions

  1. Improves the program structure because the error handling code is separated from the normal program function.
  2. Improves the program structure because the programmer can choose where to handle exceptions.
  3. Allows the creation of new exceptions that are tailored according to specific need.

Errors Errors are subclasses of Error. They are used to indicate linkage errors and virtual machine errors. Because errors are unrecoverable, they should not be caught.
Runtime exceptions The subclasses of RuntimeException are collectively referred to as runtime exceptions. These exceptions are not required to be declared because practically any method can generate them and it would be tedious to have to declare them. Examples of runtime exceptions include NullPointerException and IndexOutOfBoundsException.
Checked exceptions Checked exceptions are any subclass of Throwable that is not a subclass of Error or RuntimeException.
Checked exceptions must be declared.


The Exception Class

Exceptions of type Exception are known as checked exceptions. If code can throw an Exception, you must handle it using a catch block or declare that the method throws that exception, forcing the caller of that method to handle that exception.

Listing 5-2. CheckedExceptionExample1.java
import java.io.*;
class CheckedExceptionExample1 {
 public static void main(String []args) {
  FileInputStream fis = new FileInputStream(args[0]);
 }
}

This program results in a compiler error.
CheckedExceptionExample1.java:5: error: unreported exception FileNotFoundException; must be caught
or declared to be thrown
FileInputStream fis = new FileInputStream(args[0]);
^ 1 error

Java Exception Question:


What happens when an exception is thrown from the try-with-resources block and another exception is thrown while closing the resource of the try-with-resources block?
  1. The exception thrown while closing the resource gets propagated further while the exception thrown from the try block is suppressed.
  2. The exception thrown from the try block is propagated further, while the exception thrown while closing the resource is suppressed.
  3. The exception thrown while closing the resource is added as a cause to the exception thrown from the try block and the latter is propagated further.
  4. The exception thrown from the try block is set as a cause for the exception thrown while closing the resource and the latter one is propagated further.

Answer: b
In the try-with-resources statement, the exception thrown while closing a resource is suppressed. The exception thrown from the try block is propagated further. In Java SE 7 and later, you can retrieve suppressed exceptions.
This module focuses primarily on how to deal with normal exceptions.