• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

custom exception

 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry for your help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic