This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

code does not compile for exceptions

 
Chandra Bairi
Ranch Hand
Posts: 152
  • 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: 24212
35
Chrome Eclipse IDE Mac OS X
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic