File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

code does not compile for exceptions

 
Chandra Bairi
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class AQuestion
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(java.io.IOException t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
why does this not compile and why does this compile safely

public class AQuestion6
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(IllegalStateException t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}

for what exceptions do compilation succeed and for which the compilation deos not succeed when there are no lines in the try block?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Checked" exceptions are those that extend Throwable directly, or extend Exception but not RuntimeException. You cannot catch a "checked" exception type unless there is some possibility of its being thrown -- i.e., you can't catch one after an empty try block.
All other exception types are "unchecked" exceptions, and the Java compiler does not, in general, worry about whether or not these can be thrown.
See the Java Language Spec, chapter 11.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic