Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The throw question

 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When I compile this, i get,

Adding 'throws MyException' to main() would solve this, but why am I getting this error in the finally block,while a very similar throw inside catch block works fine.
I just want JVM's default handler to catch the exception I throw from finally. Same with catch is very fine. Default handler gets it.

Is there something i'm doing wrong? Is there any helping explanation?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Very similar" does not mean "the same".

An NPE is a RuntimeException. MyException is not. Checked exceptions *must* be either caught, or declared to be thrown. Runtime exceptions require neither.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks very similar to what is discussed in this thread.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David & Campbell...I completely get that Checked/UnChecked Exceptions
I just have one more question in store, regarding this.
I tried extending 'RuntimeException' class instead of 'Exception' so that my custom exception could be an unchecked one, but i'm getting compile errors like

I just have extended RuntimeException which IS-A Throwable instance,right? though not directly. Can we not make our custom exceptions UnChecked?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We'd have to see the current code in order to help.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops..my mistake,my mistake. works fine when i subclass RuntimeException.
I just used 'RunTimeException' instead of 'RuntimeException'. I got some 7 compile errors..didn just focus on the very first one - cannot find symbol.
really am sorry to bother you people.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic