• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checked Exception

 
Selvi Ranjan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kareem Qureshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You very much. It clear now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic