Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes code does not compile for exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "code does not compile for exceptions" Watch "code does not compile for exceptions" New topic
Author

code does not compile for exceptions

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
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?


Thanks,
Shekar
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

"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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: code does not compile for exceptions
 
Similar Threads
Exception
exception
Exception doubt
Throwable, Exception and IOException
Why so much difference between two similar class?