October 1, 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, when the the code encounters the divide by zero, the application does not catch it and the exception finds its way to the run-time and terminates the application ungracefully as seen in Figure 1.



Click here for a larger image.

Figure 1: Generating an anomaly.

There is enough information presented when the application crashes that you can see that the system has recognized an ArithmeticException.

Because this is the first anomaly you will explore (actually an Exception), it is interesting to inspect the inheritance tree of the ArithmeticException.

java.lang.Object
   java.lang.Throwable
      java.lang.RuntimeException
         java.lang.ArithmeticException

ArithmeticException actually is a direct descendant of RunTimeException.

Here is the API definition for the RunTimeException:

public class RuntimeException
extends Exception

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.

One of the important issues here is defined in the second sentence. For a RunTimeException, you do not have to do any preparation for it in your code. In short, this means that the compiler does not require you to provide code to catch a possible occurrence of a RunTimeException. However, this does not mean that you shouldn't—it is just that the compiler will not enforce this.

Consider the IOException as described by the Java API:

java.lang.Object
   java.lang.Throwable
      java.lang.Exception
         java.io.IOException

Note that in this case the IOException does not inherit from RunTimeException; it is a direct descendant of Exception. In the case of IOException, the compiler will force you to prepare for this in the sense that you will have to provide code to catch or throw the IOException. To illustrate, consider the following code in Listing 2:

import java.io.*;

class OpenFile {
   public static void main(String args[]) {

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

      DataInputStream fileIn =
         new DataInputStream(fileStream);

      while (fileIn .available() !=0) {

         System.out.println (fileIn .readLine());

      }

      fileIn .close();

   }

}

Listing 2: Generating an IOException.

When you compile this code, you receive the output in Figure 2.



Click here for a larger image.

Figure 2: Generating an IOException.





Page 2 of 6



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel