Friday 30 November 2012

EXCEPTION HANDLING CONT2........

 EXCEPTION HANDLING CONT2........
EXCEPTION MATCHING:
     If you have different framework consisting of a super class exception and a variety of subtypes and you've enthusiastic about managing one of the subtype in unique way but want to deal with all the relax together you need create only two capture conditions. When different is tossed coffee will try to discover (by looking at the available capture conditions from the top down) a capture stipulation for the exception kind if it doesn't discover one it will look for for a owner for a super type of the exception then the exception is spread down the contact collection. This procedure is known as exception related. Let's look at example:

    transfer coffee.io.*;
    community category readData {
    community fixed gap main(String args[]) {
    try {
    RandomAccessFile raf =
    new RandomAccessFile("myfile.txt","r");
    byte b[] = new byte[1000];
    rar.readfully(b, 0, 1000);
    }
    catch(FileNotFoundException e) {
    System.err.println("File not found");
    system.err.println(e.getmessage());
    e.printStackTrace();
    }
    catch(IOException e) {
    system.err.println("IO Error");
    system.err.println(e.tostring());
    e.printstacktrace();
    }
    }
    }


    This brief program efforts to start a information file and to study some information from it starting and studying information files can produce many exception most of which are some kind of IOException suppose in this software we're enthusiastic about understanding only whether the actual exception is a filenotfoundException. Otherwise we don't care exactly what the issue is

       FileNotFoundException is a subclass of IOException. Therefore we could manage it in the capture stipulation that conditions that grabs all subtypes of IOException but we would have to check the exception to figure out whetherit was a FileNotFoundException. Instead we known as a unique exception owner for the FileNotFoundException and a individual exception owner for all other
IOException subtypes.
       If this value produces a FileNotFoundException it will be managed by the capture stipulation that starts at range 10 if it produces another IOException-perhaps EOFException which is a subclass of IOException-it will be managed by the capture stipulation that starts at line15. If some other exception is produced such as a playback exception will be spread down the contact collection.

      Observe that the capture stipulation for the File Not Found Exception was placed above the owner for the IOException. This is really necessary if we do it the other way this method will not gather. The handlers for the most particular exclusions must always be placed will not gather the handlers for the most unique exclusions must always be placed above those for more common exclusions. The following will not compile:

       try {
       // do dangerous IO things
      } capture (IOException e) {
      // manage common IOException
      } capture (FileNotFoundException ex) {
      // manage just FileNotFoundException
      }


YOU'LL GET A COMPILER ERROR SOMETHING LIKE THIS:
TestEx.java:15
: exception coffee.io.FileNotFoundException has already been caught
} capture (FileNotFoundException ex) {

      If you think returning to the individuals with football safety gloves (in the area "propagating Uncaught Exception") suppose the most common safety gloves are the biggest and can thus capture many different types paint balls. An Ioexception glove is huge enough and versatile enough to capture any kind of IOException so if the individual on the fifth ground (say, Fred) has a big 'ol IOException glove he can't help but capture a File Not Found Exception football with it,

    And if the guy (say,jimmy) on the second ground is having a File Not Found Exception glove that   File Not Found Exception football will never get to him since it will always be ceased by Sam on the fifth ground status there with his big-enough-for-any-IOException glove so what do you do with exception that are friends in the other then the transaction in which the capture stipulation re placed doesn't issue.

No comments:

Post a Comment