• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

"Exception is never thrown in body of corresponding try statement." When does this error occur?

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the Compiler error "IOException is never thrown in body of corresponding try statement" for the below code. But I have got no error when I caught "NumberFormatException" instead of "IOException". When does this error occur? And when does it not??


import java.io.*;

public class abc
{

public static void main(String args[]) throws Exception
{
try
{
System.out.println("Try ");
}
catch (IOException e) {}
finally
{
System.out.println("Finally ");
}
}
}
 
Greenhorn
Posts: 25
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IOException can never occur from System.out.println(). But NUmberFormatException can occur in the following case :

I hope this clears your doubt.
 
Whizlabs Java Support
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bindu ,

If the underlying stream is a socket stream and the network dies while you're reading, if you're attempting to read from a stream that's been closed, etc.There are many instances where the IOException will be raised. If you want to raise the IOException you can do it explicitly by using keyword throw.Look at the following code which will throw the IOException explicitly.

 
Bindu Tadivaka
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Koushik and James.

What I understood is that, Compiler does not generate an Error if we try to catch an Exception that might possibly be thrown in the try block. But if there is absolutely no possibility of that Exception occurring in the try block, then the Compiler would complain.

I guess an empty try block guarantees no exceptions, whatsoever. In this case, compiler should complain if we try to catch any kind of Exception. But, I still see no error in case of NumberFormatException and error in case of IOException. Why is this discrepancy?

try
{

}
catch (NumberFormatException e) {}

finally
{
System.out.println("Finally ");
}
 
author
Posts: 23960
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bindu Tadivaka wrote:Thank you Koushik and James.

What I understood is that, Compiler does not generate an Error if we try to catch an Exception that might possibly be thrown in the try block. But if there is absolutely no possibility of that Exception occurring in the try block, then the Compiler would complain.

I guess an empty try block guarantees no exceptions, whatsoever. In this case, compiler should complain if we try to catch any kind of Exception. But, I still see no error in case of NumberFormatException and error in case of IOException. Why is this discrepancy?





The compiler only error checks checked exceptions -- hence, the name. IOException is a checked exception. NumberFormatException is an unchecked exception.

Henry
 
Bindu Tadivaka
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Henry :-)

What I finally understood is, if we catch a checked exception, that has no possibility of being thrown in the try block, compiler would generate an error. In case of unchecked exception, even if there is no possibility of it being thrown in the try block, compiler does not complain.

I hope I am correct this time :-)
 
I was her plaything! And so was this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic