aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Checked Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Checked Exception" Watch "Checked Exception" New topic
Author

Checked Exception

Selvi Ranjan
Greenhorn

Joined: Mar 16, 2002
Posts: 9
Hello Everyone,
please check this code
import java.io.IOException;
class testing
{
public void fo()
{
try
{
fo1();
} catch (Exception e)
{
}
}
public void fo1()
{

}
}
The above class compiles fine eventhough method fo1 doesn't throw any Exception object.
But
import java.io.IOException;
class testing
{
public void fo()
{
try
{
fo1();
} catch (IOException e)
{
}
}
public void fo1()
{

}
}
This class won't compile error
Exception java.io.IOException never thrown in the corresponding try statement.
I think both Exception and IOException are checked exceptions and Exception is the superclass of IOException. My question is why the first program compiles since Exception is of checked exception. Help me please
Thanks in advance
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Yes that may seem weird but there is a statement in the JLS that makes it very clear:
JLS 11.5 The Exception Hierarchy

The subclasses of Exception other than RuntimeException are all checked exception classes.

Note that it says that all subclasses of Exception are checked exceptions, which rules out the class Exception itself.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Kareem Qureshi
Ranch Hand

Joined: Mar 14, 2002
Posts: 102
Hi,
Exeption class is root of all the exceptions(not errors) runtime as well as checked exceptions and in case of runtime exceptions it doesnt matter if you catch an exception or not(also if ur method throws runtime exception you may or may not catch it in a try catch block.
In first case the compiler is thinking that it is kind of runtime exception and thus it is compiling.
But in 2nd case IOException is a concrete checked Exception, the compiler is throwing an exception stating that the exception is never reached which is true.
Try to throw an ArithmeticException for the method and dont provide the try/catch block it will compile because ArithmeticException is subclass of runtime exception.
Hope you understood. This is a greenhorns explanation. Val can you shed some light on this explanation
Kareem
Selvi Ranjan
Greenhorn

Joined: Mar 16, 2002
Posts: 9
Thanks Valentin
But it is still confusing
Note that it says that all subclasses of Exception are checked exceptions, which rules out the class Exception itself
Is that means that Exception is unchecked exception. If so Check the following program
import java.io.IOException;
class testing
{
public void fo()
{
throw new Exception("ddd");
}
public void fo1()
{

}
}
This gives a compilation error that Exception should be catch or thrown. So this proves that the Exception is checked exception.
As per my knowledge a method must either catch or throw checked exception whereas unchecked exceptions doesn't require that rule.
Thanks
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
In fact, this is a bug (ID 4046575), fixing in progress since 1997!.
The following page is also worth reading:
Unofficial JLS errata: 14.20
Selvi Ranjan
Greenhorn

Joined: Mar 16, 2002
Posts: 9
Thank You very much. It clear now
 
Don't get me started about those stupid light bulbs.
 
subject: Checked Exception