Chapter 12. Exception Handling


A lot can go wrong when a program runs. The user can provide inappropriate input data, a system or network error might occur, or there might simply be a bug in the code that you didn't know was there. Exception handling allows a program to continue running when an exception occurs. The exception is detected and processed in such a way that the program can keep going.

There is no built-in exception handling in Fortran or C. If you want exception handling in these languages, you have to do all the programming work yourself which usually involves a potentially complicated set of if-then statements and error codes. You also have to perform the exception handling where the exception will occur. You can't easily pass the exception handling to another part of your program.

Java has a sophisticated exception handling capability built into the language. When a problem occurs inside a Java program, an exception object is thrown. The Java runtime then will try to send the exception to an appropriate section of code that can handle the exception. The section of code can try to correct the problem or can gracefully terminate the program. Java exception handling lets you decide where the exception handling will occur. An exception thrown by one method can be passed back to a calling method. You can also define your own exception classes for any specific needs your program might have.

There are two general types of Java exceptions ”checked and unchecked. Checked exceptions are those for which the Java compiler checks for the presence of exception handlers. An example of a checked exception is an IOException that can occur if there is a problem with an I/O operation. The compiler will make sure that all checked exceptions are either caught in a catch clause or declared in a throws clause. If you don't catch or declare a checked exception, the program won't compile.

Unchecked exceptions are more generic and can happen in a number of circumstances. Exceptions that occur at runtime, such as an ArithmeticException , are treated as unchecked exceptions. System errors that can't usually be recovered from by an application are also not checked. The compiler will not ensure that unchecked exceptions are caught or declared. If you don't handle an unchecked exception inside a catch clause, the default system handler will take care of it by terminating the program.

There are two basic ways of handling exceptions. You can process the exception where it happens by providing a try statement or you can pass the exception to another part of your program for processing by using the throw statement and throws clause.

In this chapter we will discuss ”

  • The Exception class hierarchy

  • try statements

  • The throw and throws keywords



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

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