1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

variable exception in java

Discussion in 'Java' started by 2007160935, Oct 23, 2012.

  1. 2007160935

    2007160935 New Member

    Joined:
    Mar 6, 2009
    Messages:
    38
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    ..
    Location:
    ..
    why do you need a variable in catch exception and how do you use each it the same as the detailed exception?

    TemperatureException and e
    TooColdException and e

    for example:

    try {
    cust.drinkCoffee(cup);
    System.out.println("Coffee is just right.");
    }
    catch (TemperatureException e) {
    // This catches TooColdException, TooHotException,
    // as well as TemperatureException.
    System.out.println("Coffee is too cold or too hot.");
    // Deal with an irate customer...
    }
    // THIS WON'T COMPILE, BECAUSE THIS catch clause
    // WILL NEVER BE REACHED.
    catch (TooColdException e) {
    System.out.println("Coffee is too cold.");
    }
     
  2. state

    state New Member

    Joined:
    Sep 14, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Hi 2007160935
    Whenever an exception arises it is handled by the JVM(completely or partially).Since the JVM is taking the pain to throw the error there must be something to store the error in and it is known as an Exception object.This is the why.How?override toString().
     
  3. alia123

    alia123 New Member

    Joined:
    Jan 8, 2016
    Messages:
    65
    Likes Received:
    3
    Trophy Points:
    0

Share This Page