Friday 30 November 2012

EXCEPTION HANDLING CONT3.....

RETHROWING THE SAME EXCEPTION:
        Just as you can toss a new exemption from a capture agreement you can also toss the same exemption you just captured here's capture stipulation that does this.

   catch(IOException e) {
   // do factor then if you choose you can't manage it
   toss e;
   }

      All other capture conditions associated with the same try are ignored if a lastly prevent leaves it operates and the exemption is tossed returning to the contacting technique (the next technique down the contact stack). If you toss a examined exemption from a capture stipulation you must also announce that exemption in other terms you must manage ad announce in contrast to deal with or announce the following example is illegal:
     community gap dostuff() {
     try {
     // dangerous IO things
     } catch(IOException ex) {
     // can't manage it
     toss ex; // can't toss it unless you announce it
    }
     }

  In the previous value the doStuff() technique is clearly able to toss a examined exemption in this situation an IOException so the compiler says "well that's just wonderful that you have a try/catch in there but it's not excellent enough. If you might rethrow the IOExcepion you capture then you must announce it

CREATING AN EXCEPTION:
         In this work out we create an effort to create a customized exemption we won't put in any new technique (It will have only those got from exception) and because expands exemption the compiler consider it a examined exemption the objective of the system is to figure out whether a control range discussion comprising a particular meals (as a string) is regarded bad or OK.

    Let's first create our exemption we will contact it BadFoodException this exemption will be tossed when a bad meals is experienced.
    create an linking category known as MyException and a min() technique which will stay vacant for now.

RETHROWING THE SAME EXCEPTION:
    Build a technique known as checkFood(). It requires a sequence justifications and punches our exemption if it doesn't like the meals it was given. Otherwise it informs us it prefers the meals you can add meals you aren't particularly attached to to the record.
    Now in the main() technique you'll get command-line discussion out of the sequence range and then finish that sequence on to the CheckFood() technique. Because it's examined exemption the checkFood() technique must announce it and the main() technique must manage it (using a try/catch). Do not have main() announce the exemption because if main() geese the exemption who else is returning there to capture it?
    As awesome as exemption managing creates dealing with is it's still up to designer to create appropriate use of it. Exception managing creates planning our value and signaling issue simple but the exemption owner still have to be published. you'll discover that even the most complicated circumstances can be managed and your value will be recyclable understandable and maintainable.

No comments:

Post a Comment