Proper exception handling in your program will enhance its robustness and reliability. The try
, catch
, and finally
blocks can be used to implement exception handling within an application. In Java 7, the try-with-resources block has been added which more easily handles the opening and closing of resources. It is also possible to propagate an exception back up the call sequence.
We learned that the order of catch blocks is important in order to properly handle exceptions. In addition, the |
operator can be used in a catch block to handle more than one exception in the same way.
Exception handling may be nested to address problems where the code within a catch, or finally block, may also throw an exception. When this happens, the programmer needs to be careful to insure that previous exceptions are not lost and that the new exceptions are handled appropriately.
We also addressed a number of common problems that can occur when handling exceptions. They provided guidance as to avoid poorly...