• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Exception Handling

 
ayush raj
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should be the output ?


I thought it to be Runtime exception since a number is divided by zero . But actually compilation fails !! How?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And compilation fails with what error message?
 
ayush raj
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the error message is :

exception java.lang.InterruptedException is never thrown in body of corresponding try statement

Is it that that what ever exception is thrown inside a try block , the first catch has to be the corresponding class exception handler ??

 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the problem. InterruptedException is a checked exception. The compiler therefore knows that it cannot possibly be thrown from the try block, so it knows that trying to catch it is an error. Runtime exceptions don't have the same effect (even though in this case theoretically the compiler could work out that ArrayIndexOutOfBoundsException can't be thrown).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic