• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Try - Catch Exception

 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh sorry, I was trying different Exception, probably copied the wrong error from my editing tool "Textpad"
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic