Chapter 17. Exception Handling


Terms you'll need to understand:

  • Exception object

  • Error object

  • try-catch-finally construction

  • Exception chaining

Techniques you'll need to master:

  • Identifying problems in Java source code files

  • Handling exceptions and errors

  • Removing logic bugs that don't throw an exception

  • Writing code that distinguishes between errors and exceptions

  • Giving code corrections for various source code problems in a Java application

In this chapter, you review how to handle exceptions. Java has two types of runtime errors: errors and exceptions. An error originates from the Java Virtual Machine (JVM) and indicates an imminent crash. An exception originates from the code and indicates an exceptional circumstance that needs to be handled by a try-catch-finally block. Only if an exception is not handled does the application crash. Remember that you can't recover from an error that causes your application to crash. You can, however, recover from an exception with proper use of the try-catch-finally construction.

When adding exception handling to your certification solution, you should bear in mind the higher concept of problem resolution in software. Approaching exception handling first from this higher ground eases the burden of quality control later. In other words, there are steps you can take to reduce bugs during the design phase.

Figure 17.1 illustrates the relationship between different types of errors in Java. Note that all errors and exceptions in Java are subclasses of the Throwable class. The primary characteristic is whether Java checks for that type of error.

Figure 17.1. The Throwable class hierarchy.

graphics/17fig01.gif

The following list describes the characteristics of errors and exceptions and explains the differences between them:

  • The Throwable class is the superclass of all Error and Exception classes.

  • Errors are usually fatal and not caught. Let the JVM handle them.

  • There are two types of exceptions: checked and unchecked. RuntimeException and Error , and their subclasses, are unchecked. All instances of Exception and its subclasses are checked.

  • Checked exceptions (subclasses of Exception ) must be declared (method declaration) or caught and are recoverable. The compiler complains if these exceptions are not caught, so you must handle them.

  • Unchecked exceptions (subclasses of RuntimeException ) are usually fatal and not caught. Let the JVM handle them.

The preceding list describes the code- related errors you must be aware of. However, there are other types of problems that software engineers must contend with in addition to errors and exceptions. The next section discusses these problems from a broader perspective.



JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 187

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net