Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

code does not compile for exceptions  RSS feed

 
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
Sheriff
Posts: 24217
38
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
Boost this thread!