mousemat212
asked on
Difference between throwing and catching an exception?
Hi,
What is the difference between throwing an exception and catching an exception?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
yes, when you catch the exception in a catch block you'll information from the stack trace in your variable (in my example "e"), so you can still display your stack without your app dying.
ASKER
What is the difference between:
private void myMethod() throws IOException
and
private void myMethod()
{
//do stuff
}
catch(IOException e)
{
e.printStackTrace();
}
?
ASKER
Assuming of course myMethod throws a check exception.
ASKER
>>Assuming of course myMethod throws a check exception.
sould be
Assuming of course myMethod throws a checked exception.
Thanks!
sniff... sounds like homework.
private void myMethod() throws IOException
this is the originator
let's say it is in class mousemat212
Now the following is in mousemat140
private void myMethod()
{
try{
//do stuff
x = new mousemat212();
x.myMethod();
//code 2
}catch(IOException e){
e.printStackTrace();
}
at this point, if mousemate212.myMethod simply throws an IOException, e.printStackTrace will be ran instead code 2
ASKER
>>sniff... sounds like homework.
Nope its not homework.
This is my complete method:
private void setAmt(HttpSession session) throws IOException
{
Properties properties = new Properties();
String amtItemsPerPage = null;
try
{
properties.load(new FileInputStream("myProp.pr
number = properties.getProperty("**
Integer.parseInt(number);
}
catch (NumberFormatException numberFormatException)
{
log.error("Could not convert value to Integer, numberFormatException);
}
catch (FileNotFoundException notFoundException)
{
log.error("Could not find property at file",notFoundException);
}
}
I was just getting confused at what the point was of throwing an IOException (the compiler forces me too) when im catching the exceptions that im worried about.
Sometimes the compiler needs to know that a certain kind of exception will be thrown (although you can specify "throws" on any kind of class).
Have a look at: http://www.artima.com/designtechniques/exceptions.html and the Exceptions part of here is quite good too: http://www.cs.wisc.edu/~cs536-1/NOTES/JAVA/Exceptions.html
Alternatively Google: "in java why throw an exception", some relevent hits come up.
Have a look at: http://www.artima.com/designtechniques/exceptions.html and the Exceptions part of here is quite good too: http://www.cs.wisc.edu/~cs536-1/NOTES/JAVA/Exceptions.html
Alternatively Google: "in java why throw an exception", some relevent hits come up.
ASKER
Thanks everyone
ASKER
Thanks for the replys,
ok, so throwing an exception is like letting the jvm display the stack trace, if the exception is thrown, and catching an exception is performing your own custom handling code to deal with the exception?