September 2, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Object Integrity & Security: Error & Exceptions

  • September 6, 2007
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

In this case, anything that is thrown will be caught in this try/catch block. There is no granularity when it comes to Errors, Exceptions, RunTimeErrors, and so forth. As far as information reporting goes, note that in this example, you are using the following line inside the catch block itself.

System.out.println(e);

This technique is powerful when your code is in development and/or testing. You can start out by casting a wide net and reporting exceptions and errors at a very high level. However, as the development progresses and testing uncovers various issues, you then can use this information to drill down and handle the various situations that arise. In Figure 6, the printed output actually indicates the specific exception that was encountered—in this case, an ArithmeticException.



Click here for a larger image.

Figure 6: Catching all things Thrown.

You also can use a 'compound' catch block to catch more than one specific exception, as seen below.

   try {

      ....

   } catch (java.lang.ArithmeticException e) {

      System.out.println(e);

   } catch (java.io.IOException  e) {

      System.out.println(e);

   }

Listing 7 shows an application that combines the two exceptions that I have used in your illustrations, ArithmeticException and FileNotFoundException.

import java.io.*;

public class Anomaly {

   public static void main(String args[]){

      int a = 0;
      int b = 1;
      int c = 0;

      System.out.println("Begin Application");

      try {

         System.out.println("Divide by Zero");

         c = a/0;

         FileInputStream fileStream= new
            FileInputStream("Test.txt");

         DataInputStream fileIn =
            new DataInputStream(fileStream);

         while (fileIn .available() !=0) {
            System.out.println (fileIn .readLine());

         }

         fileIn .close();

      } catch (java.lang.ArithmeticException e) {

         System.out.println(e);

      } catch (java.io.IOException  e) {

         System.out.println(e);

      }

      System.out.println("Exit");

   }

}

Listing 7: Catching more than one individual exception.

The main issue to understand here is that the exceptions will be handled in the order that they are encountered in the catch block. This is important because, if you do something like this, some of the code is considered redundant. For example, in the following code stub, because Exception is listed first, all other exceptions listed in the block (in this case java.io.IOException) are, in fact, redundant. In short, if an IOException is encountered, it will actually be caught by the Exception catch block (because it is indeed an exception) and not even find its way to the IOException catch block.

   try {

      ....

   } catch (Exception e) {

      System.out.println(e);

   } catch (java.io.IOException  e) {

      System.out.println(e);

   }




Page 5 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel