File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Never Thrown error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Never Thrown error" Watch "Exception Never Thrown error" New topic
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Never Thrown error
 
Similar Threads
Exceptions in Finally section
try without catch
try..catch flow???
Finally
One more on exceptions