• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Exception and its checked Exception subclasses

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class ExceptionTest {
public static void main(String args[]) {
try {
ClassNotFoundException e=null;
}
catch (ClassNotFoundException e) { //compile error
//catch (Exception e) { //compile and run
System.out.println(e);
}
finally {
System.out.println("Finally");
}
}
}
For the above program, catch (Exception e) compile and run, but
catch (ClassNotFoundException e) has the following compile error:
Exception java.lang.ClassNotFoundException is never thrown in the body of the corresponding try statement.
I think java.lang.Exception is never thrown in the body of the corresponding try statement too, but why it doesn't create the compile error?
Thanks.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bin,
You might be getting lost in syntax here. The compiler is telling you that your try/catch/finally block is looking for an exception that is not being thrown.
What does that mean? It means that in order to catch an exception something inside the try section must throw it! In your case, your try block doesn't throw any exception, but you want to catch something with your catch statement.
The JLS only states that if you try and catch an exception it had better be thrown somewhere. It doesn't say to use a try/catch/finally block your code should throw an exception inside the try statement!
Think about a case where you want to perform something at the end of a method in most error cases. We could do the following:

Regards,
Manfred.
[This message has been edited by Manfred Leonhardt (edited June 18, 2001).]
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manfred:
Thanks for your explanation, but could you please explain why the following program compiles and runs fine although its try block doesn't throw any exception at all?
public class ExceptionTest {
public static void main(String args[]) {
try {
Exception e1=null;
ClassNotFoundException e2=null;
Exception e3=new Exception();
ClassNotFoundException e4=new ClassNotFoundException();
}
catch (Exception e) {
System.out.println(e);
}
finally {
System.out.println("Finally");
}
}
}
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manfred, can you point me to the reference in JLS that deals with trying to catch an Exception which is not thrown by the associated try block? I checked chapters 11 & 14 but couldn't find this information.
Alternatively, what exception could be thrown by the statement "ClassNotFoundException e=null;"? If this statement does indeed throw an exception of some sort, then I understand why "catch (Exception e)" compiles whereas "catch (ClassNotFoundException e)" fails to compile.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bin,
The reason your second code compiles, is because you are catching Exception (the superclass of all exceptions). The compiler doesn't know if a run-time exception will happen at compile time so it MUST let the code compile. Whereas with ClassNotFoundException is a specific exception and the compiler can easily figure out if it is being thrown.
Regards,
Manfred.
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manfred,
Thank you very much. I understand it now.
reply
    Bookmark Topic Watch Topic
  • New Topic