Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   variable exception in java (http://www.go4expert.com/forums/variable-exception-java-t29216/)

2007160935 23Oct2012 13:19

variable exception in java
 
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.");
}

state 14Nov2012 16:24

Re: variable exception in java
 
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().


All times are GMT +5.5. The time now is 18:04.