This page is optimized for mobile devices, if you would prefer the desktop version just click here

2.15 Java1630: exception handling  (Page 8/17)

Another possible InterruptedException

As was the case in the previous program, this program also illustrates a failure to catch or declare an InterruptedException . However, in this case, the problem has moved up one level in the call stack relative to theproblem with the program in Listing 1 .

This program also fails to compile, producing a compiler error similar to that shown in Figure 4 . Note that the caret indicates that the problem is associated with the call to myMethod .

Figure 4 . Another compiler error.
unreported exception java.lang.InterruptedException;must be caught or declared to be thrown obj.myMethod();^

Didn't solve the problem

Simply declaring a checked exception doesn't solve the problem. Ultimately, the exception must be handled if the compiler problem is to be solved.

(Note, however, that it is possible to declare that the main method throws a checked exception, which will cause the compiler to ignore it and allowyour program to compile.)

The program in Listing 2 eliminated the compiler error identified with the call to the method named sleep . This was accomplished by declaring that the method named myMethod throws InterruptedException . However, this simply passed the exception up the call stack to the next higher-levelmethod in the stack. This didn't solve the problem, it simply handed it off to another method to solve.

The problem still exists, and is now identified with the call to myMethod where it will have to be handled in order to make the compiler error go away.

Sample program that fixes the remaining compiler error

The version of the program shown in Listing 3 fixes the remaining compiler error. This program illustrates both declaring andhandling a checked exception. This program compiles and runs successfully.

Listing 3 . Sample program that fixes the remaining compiler error.
/*File Excep13.java Copyright 2002, R.G.BaldwinTested using JDK 1.4.0 under Win2000 **************************************/import java.lang.Thread; class Excep13{public static void main( String[]args){ Excep13 obj = new Excep13();try{//begin try block obj.myMethod();}catch(InterruptedException e){ System.out.println("Handle exception here"); }//end catch block}//end main //---------------------------------//void myMethod() throws InterruptedException{Thread.currentThread().sleep(1000); }//end myMethod}//end class Excep13

The solution to the problem

This solution to the problem is accomplished by surrounding the call to myMethod with a try block, which is followed immediately by an appropriate catch block. In this case, an appropriate catch block is one whose parameter type is either InterruptedException , or a superclass of InterruptedException .

(Note, however, that the superclass cannot be higher than the Throwable class in the inheritance hierarchy.)

The myMethod method declares the exception

As in the previous version, the method named myMethod (declares the exception and passes it up the call stack to the method from which it wascalled.

<< Chapter < Page Page > Chapter >>

Read also:

OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.