Main Page

Previous Next


In this chapter you have learned what exceptions are and how to deal with them in your programs. You should make sure that you consider exception handling as an integral part of developing your Java programs. The robustness of your program code depends on how effectively you deal with exceptions that can be thrown within it.

The important concepts we have explored in this chapter are:

  • Exceptions identify errors that arise in your program.

  • Exceptions are objects of subclasses of the class Throwable.

  • Java includes a set of standard exceptions that may be thrown automatically, as a result of errors in your code, or may be thrown by methods in the standard classes in Java.

  • If a method throws exceptions that aren't caught, and aren't represented by subclasses of the class Error, or by subclasses of the class RuntimeException, then you must identify the exception classes in a throws clause in the method definition.

  • If you want to handle an exception in a method, you must place the code that may generate the exception in a try block. A method may have several try blocks.

  • Exception handling code is placed in a catch block that immediately follows the try block that contains the code that can throw the exception. A try block can have multiple catch blocks that deal with different types of exception.

  • A finally block is used to contain code that must be executed after the execution of a try block, regardless of how the try block execution ends. A finally block will always be executed before execution of the method ends.

  • You can throw an exception by using a throw statement. You can throw an exception anywhere in a method. You can also rethrow an existing exception in a catch block to pass it to the calling method.

  • You can define your own exception classes that, in general, should be derived from the class Exception.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor