Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Try - Catch Exception  RSS feed

 
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??
 
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"
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!