File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 18840
    
  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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: custom exception