This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes custom exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "custom exception" Watch "custom exception" New topic
Author

custom exception

Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

I've the following code:

class MyException extends Exception {
void method1(){
doMore();
}
void doMore throws MyException{
try{
throw new MyException();
}
catch (MyException me){
throw me;
}
}
}

During compilation, I'm getting the following:
MyException.java:3: Unreported exception MyException must be caught or declared to be throw
doMore();
^

Now My Question is:

I'd declared & handled the exception (MyException) on doMore(), then why this error rises?

Thanks in Advance


Thanks, Shiv
SCJP, OCE - JSP & Servlets Developer
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

I'd declared & handled the exception (MyException) on doMore(), then why this error rises?


Yes, your doMore() method has been declared to throw MyException. This is why the compiler is checking to make sure that method1() either handles it or declares throwing it too -- as it calls the doMore() method.

BTW, your "handling" of the exception in your doMore() method, doesn't do anything. It just catches it and throws it again -- which is the same as not catching it in the first place.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Thanks Henry for your help
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: custom exception
 
Similar Threads
Exceptions doubt
exception handling
Concept on coding for Exception Handling
exam watch question
exceptions - handle or declare rule