December 21, 2014
Hot Topics:

Object Integrity & Security: Error & Exceptions

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

Note that the compiler is complaining about the fact that the code must catch or throw certain exceptions. This is in contrast to the ArithmeticException that was not required to be caught. In this case, the compiler gets even more specific and indicates the specific exception that needs to be dealt with, the FileNotFoundException.

The interesting point here is that the compiler is actually trying to help you with the integrity, and perhaps security, of your code. Imagine the things that could possibly go wrong when you attempt to open a file. The obvious problem would be that the file may not even exist. Or, if you are opening it up across a network, the network connection possibly could be down.

Although the code might work perfectly well in a test environment or even 99% of the time in a production environment, there is always a chance that the file might not be available. If you don't provide contingency code for this situation, the application will become unstable, if it doesn't ultimately crash.

As the error message indicates, you have two options to get your code to compile: throw or catch the exception.

Throwing the Exception

In this example, the option of throwing the exception will allow you to compile the application. The code is presented in Listing 3. Note the inclusion of the throws IOException statement in the definition of the main( ) method.

import java.io.*;

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

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

      DataInputStream fileIn =
         new DataInputStream(fileStream);

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

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

      }

      fileIn .close();

   }

}

Listing 3: Throwing the Exception.

When the code is compiled, you get the output in Figure 3.



Click here for a larger image.

Figure 3: Throwing the Exception.

Although this code will compile, in reality it circumvents the intent of the compiler checks regarding these types of exceptions. The issue is that the code will not properly handle the IOException if and when it actually does occur. All the application does in this case is to pass the exception along to the runtime environment. And you all know what will happen then—it will terminate the application as seen in Figure 4.



Click here for a larger image.

Figure 4: The file is not found.

As you have learned over and over, no matter what happens, you must try and make sure that the application never crashes. Thus, simply throwing exceptions must be done with care. In practice, throwing an exception in the main ( ) method is dangerous and you should only throw an exception when you know that another method up the hierarchy in your application will indeed deal with it.

Catching the Exception

In fact, the best way to deal with the situation in the example is to catch this exception. The code for this is Listing 4, which does produce a clean compile.

import java.io.*;

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

      try {

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

         DataInputStream fileIn =
            new DataInputStream(fileStream);

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

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

         }

         fileIn .close();

      } catch (Exception e) {

         System.out.println("Exception caught");

      }

   }

}

Listing 4: Throwing the Exception.





Page 3 of 6



Comment and Contribute

 


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

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel