• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception Handling  RSS feed

 
anup sachan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i try to compile the code below.the error comes as:
Test3.java:14: exception java.io.IOException is never thrown in body of corresponding try statement.
Is it necessary that exception should be thrown in the try statement.
Thanks in advance


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");
}
}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is it necessary that exception should be thrown in the try statement.

Yes. The compiler will stop you trying to catch a checked exception that is not thrown from within the try block. So this:

will not compile. But this:

compiles fine.
 
anup sachan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but below also we are not throwing amy checked exceptions .But the code compiles fine
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by anup sachan:
but below also we are not throwing amy checked exceptions .But the code

Indeed. You are not throwing (or catching) any checked exceptions, so the code compiles fine. Did you expect anything else?

The compiler only does the check for checked exceptions, but ClassCastException is not a checked exception.
[ January 16, 2007: Message edited by: Jesper Young ]
 
anup sachan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks i have got my doubt clarified.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!