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

2.15 Java1630: exception handling  (Page 9/17)

The main method handles the exception

In the new version shown in Listing 3 , the main method provides a try block with an appropriate catch block for dealing with the problem (although it doesn't actually deal with it in any significant way). This can be interpreted as follows:

  • Try to execute the code within the try block.
  • If an exception occurs, search for a catch block that matches the type of object thrown by the exception.
  • If such a catch block can be found, immediately transfer control to the catch block without executing any of the remaining code in the try block.

(For simplicity, this program didn't have any remaining code. Some later sample programs will illustrate code being skipped due to theoccurrence of an exception.)

Not a method call

Note that this transfer of control is not a method call. It is an unconditional transfer of control. There is no return from a catch block.

Matching catch block was found

In this case, there was a matching catch block to receive control. In the event that an InterruptedException is thrown, the program would execute the statement within the body of the catch block, and then transfer control to the code following the final catch block in the group of catch blocks (in this case, there was only one catch block).

No output is produced

It is unlikely that you will see any output when you run this program, because it is unlikely that an InterruptedException will be thrown. (I didn't provide any code that will cause such an exception to occur.)

A sample program that throws an exception

Now let's look at the sample program in Listing 4 , which throws an exception and deals with it. This program illustrates the implementation of exceptionhandling using the try/catch block structure.

Listing 4 . A sample program that throws an exception.
/*File Excep14.java Copyright 2002, R. G. BaldwinTested with JDK 1.4.0 under Win2000 **************************************/class Excep14{ public static void main(String[] args){try{ for(int cnt = 2; cnt>-1; cnt--){ System.out.println("Running. Quotient is: " + 6/cnt);}//end for-loop }//end try blockcatch(ArithmeticException e){ System.out.println("Exception message is: " + e.getMessage()+ "\nStacktrace shows:"); e.printStackTrace();System.out.println( "String representation is\n " +e.toString()); System.out.println("Put corrective action here"); }//end catch blockSystem.out.println( "Out of catch block");}//end main }//end class Excep14

Keeping it simple

I try to keep my sample programs as simple as possible, introducing the minimum amount of complexity necessary to illustrate the main point of theprogram. It is easy to write a really simple program that throws an unchecked ArithmeticException . Therefore, the program in Listing 4 was written to throw an ArithmeticException . This was accomplished by trying to perform an integer divide by zero.

The try/catch structure is the same ...

It is important to note that the try/catch structure illustrated in Listing 4 would be the same whether the exception is checked or unchecked. The main difference is that you are not required by thecompiler to handle unchecked exceptions and you are required by the compiler to either handle or declare checked exceptions.

<< 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.