| Author |
Exception Never Thrown error
|
Anup Engineer
Ranch Hand
Joined: Mar 04, 2002
Posts: 48
|
|
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.
|
Anup Engineer
|
 |
Alex Ku
Ranch Hand
Joined: Jan 15, 2002
Posts: 47
|
|
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
|
 |
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
|
|
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
|
SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
|
 |
 |
|
|
subject: Exception Never Thrown error
|
|
|