• Post Reply Bookmark Topic Watch Topic
  • New Topic

Try - Catch Exception  RSS feed

 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code generates a compile error:

Test.java:9: exception java.lang.InterruptedException is never thrown in body of corresponding try statement
catch(InterruptedException t)
^
1 error
If you change the IOException with Throwable or Exception or RuntimeException then no compile error occurs.
So what's the rule here?? Does the compiler only check on the Checked Exceptions if it's actually thrown or not? but not the Unchecked Exceptions??
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's one of the things it means to be a "checked" exception.
It is not legal to try to catch a checked exception that will never be thrown. It is always legal to catch an unchecked exception. It's also legal to catch Exception and Throwable, since those classes have both checked and unchecked subclasses.
(By the way, are you sure that error message comes from the code you are showing us? The code catches IOException but the error message refers to InterruptedException.)
[ October 18, 2002: Message edited by: Ron Newman ]
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh sorry, I was trying different Exception, probably copied the wrong error from my editing tool "Textpad"
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!