Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Exception Never Thrown error

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I write code in try block that never throws an "Exception" and catch it as catch(Exception e){}
it works. Basically, This works:

import java.io.*;
class NeverThrownException {
public static void main (String[] args) {
try{
return;
}
catch(Exception e){
System.out.println("caught exception");
}
finally{
System.out.println("Finally");
}
} // end of main ()

}

But I have something more specific like IOException instead of Exception it gives me a compile time error:
exception java.io.IOException is never thrown in body of corresponding try statement
catch(IOException e){
this doesnt work:
import java.io.*;
class NeverThrownException {
public static void main (String[] args) {
try{
return;
}
catch(IOException e){
System.out.println("caught IOexception");
}
finally{
System.out.println("Finally");
}
} // end of main ()

}
any ponters?
Thanks.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I believe it is due to checked/unchecked exception.
And IOException is a checked exception.
If you try ArrayIndexOutOfBoundsException, this compiles fine cuz it is an unchecked exception.
correct me if i am wrong,
kawaii
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We had a discussion on this a while ago:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=015205
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic