File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exception handling" Watch "exception handling" New topic
Author

exception handling

Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
hello,
This is the stuff from k&b book pg 252 - i was a bit confused while going thru this stuff.
class MyException extends Exception{
void someMethod(){
doStuff();
}
void doStuff throws MyException{
try{
throw new MyException();
}
catch(MyException me){
throw me;
}
}
}
Now this code is said to give an err::
unreported exception MyException; must be caught or declared to be thrown
this above err is prompted at the command line.
Now my question is though it has been declared in the doStuff() using the throws clause and also there is a try-catch block -- so why this compilation err occurs..


Mona(Varijasmom)
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

"varijasmom"-
Welcome to the JavaRanch! We don't have many rules around here -- but the one rule we all agree to when we sign up is that we'll follow the JavaRanch Naming Policy. Can you please adjust your display name to match the policy? (Basically it should be a first name AND a last name). You can change your display name here.
Thanks!


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Mukhtar Ahmed
Greenhorn

Joined: Nov 20, 2003
Posts: 9
I think it is because the method somemethod does not declare it in the throws clause. Since somemethod is calling dostuff which throws MyException, somemethod has to either handle it or put it in the throws clause.
Sagarika nair
Ranch Hand

Joined: Aug 13, 2003
Posts: 39
I agree with Mukhtar that the someMethod() should either declare that it throws the Myexception or handle it(handle or declare rule).
Since the someMethod() calls the doStuff() method which throws an Exception,you have to decare it in someMethod().So there would be no compiler error once u say someMethod() throws MyException.
Anurag Mishra
Ranch Hand

Joined: Sep 27, 2001
Posts: 133
USE super()

Anurag
Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
hello,
I got what u both intend to explain me. But is always necessary that evenif the method throws an exception which is also handled in the method -- is it necessary that the methods down the stack should declare that they too throw the exception. I always thought that if the exception is not handled then n then its necessary for the calling method needs to have the throws clause. Pls correct me if my assumptions r wrong.
Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
Originally posted by Anurag Mishra:
USE super()

Anurag

hello anurag,
i didn't get why super() is to be used???
Anish Doshi
Greenhorn

Joined: Jun 13, 2003
Posts: 23
Hey Mona,
The thing is that all the method in the same call stack should either declare it that it throws the exception or else handle it using try/catch. I know its a bit confusing at the first look towards exception. But this is the best way i can explain it to you. i too got confused on this one and even decided to leave the exception part itself . But then I read it about 10 times the whole exception part and then i was clear with this one. And then i successfully went thru the exam scoring wid a huge margin. I would say reading the un-understandable stuff agn and agn is the best way to know the stuff.
I hope this helps you.
Kind Regards,
Anish Doshi.


Sun Certified Programmer for Java platform 1.4
Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
Hello Anish,
Thanks for the encouragement and ur suggestion to read more n more.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exception handling