Java Exceptions  «Prev  Next»

Lesson 8

Handling Exceptions Conclusion

The module began by introducing you to exceptions and how they impact Java programming. You learned how exceptions are thrown to indicate that a problem has occurred in a program.
From there you moved on to see how exceptions are caught and handled. The module concluded by taking a look at the finally clause, which provides a means of guaranteeing the execution of a section of code regardless of whether an exception is thrown.

Handling multiple exceptions in the same exception handler

You should know the basic syntax for creating and using multi-catch blocks, together with the gotchas that may be used on the exam. So let us start with the basic syntax of multi-catch blocks.

Basic syntax of multi-catch block

To catch multiple exceptions in a single handler, just separate the different exception types with a vertical bar (|). The following is an example of a try block that defines code that can handle FileNotFoundException and SQLException (or any of their subclasses) using a multi-catch block:

finally block can follow multi-catch block
finally block can follow multi-catch block


In the preceding code, the exception handler will execute if the code throws File-NotFoundException, SQLException, or any of their subclasses.

Finally block can follow multi-catch block

A multi-catch block can be followed by a finally block. Here’s an example:
class MultiCatchWithFinally {
 void myMethod(Connection con, String fileName) {
  try {
   File file = new File(fileName);
   FileInputStream fin = new FileInputStream(file);
   Statement stmt = con.createStatement();
  }
  catch (FileNotFoundException | SQLException e) {
   System.out.println(e.toString());
  }
  finally {
   System.out.println("finally");
  }
 }
}

Glossary Terms

This module discussed how the following terms relate to Java:
  1. Exception: An exceptional situation that a program does not know how to handle is known as an exception. An example of an exception is attempting to divide a number by zero or the system running out of memory.
  2. Throwing (an exception): When an exceptional condition is encountered in a Java program, an exception is thrown, which means that a special exception object is created and passed out of the current context. The idea is that a higher context will be more capable of handling the exception.
  3. Catch clause: The catch clause is the part of the try-catch construct that is used to catch any exceptions that may be thrown by code in the try clause.
  4. Finally clause: The finally clause is used in conjunction with the try-catch construct to place code that is always executed regardless of whether an exception is thrown.
Do you have any questions or comments about the material covered so far in this course? Click the Search button on the top of the page.


Which exception or error is thrown if there is a problem when the JVM tries to initialize a static variable or run code of a static initialization block?
  1. ClassInitializationError
  2. ExceptionInInitializerError
  3. StaticInitializerError
  4. AssertionError
  5. ClassDefMissingException


Answer: b
Explanation:
Besides options b) and d), no other errors or exceptions exist as part of the standard Java classes.
AssertionError is thrown in case an assertion fails. If an exception occurs during the execution of a static initialization block, then an ExceptionInInitializerError is thrown. But if an exception occurs during a regular initialization block's execution, then that exception is propagated back to the calling method.

Handling Exceptions - Quiz

Click the Quiz link below to test what you have learned in this module.
Handling Exceptions - Quiz