• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception Exception is not compatible with throws clause in MyClass.myMethod()  RSS feed

 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try to throw an Exception and I cannot compile due to this error:

"Exception Exception is not compatible with throws clause in MyClass.MyMethod()"


the mythod:

public void myMethod() throws Exception {

myMethod2();
}

public void myMethod2() throws Exception {
}


Thanks.
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the code you have posted, it is not clear where your error is. The following compiles with no error
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

I changed to throw RuntimeException and now it's OK.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bruce Jin:
I changed to throw RuntimeException and now it's OK.
That works because all subclasses of RuntimeException may be thrown without being declared in throws clauses. For example, OutOfMemoryError, NullPointerException, StackOverflowException, and of course ...

SpanishInquisitionException, because no one expects the Spanish Inquisition!
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David.
I have forgotten this basic concept and put "throws RuntimeException " in my method singnature!
I know it is not recommended catch RuntimeException but sometimes I feel a need to do it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!