Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
    Bookmark Topic Watch Topic
  • New Topic

Compiler time error with respect to exception handling

 
Sirish Kumar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi All,
This is with respect to the exception handling rules. I understand
that any method that can throw a checked exception must take one of the
following approaches
1.Declare the checked excpetion in the throws clause OR
2.Provide the try/catch clause
Now consider the following method
//Approach 1
public void raise() throws IOException
{
//empty method
}

//Approach 2
public void raise()
{
try
{
//empty block
}
catch(IOException a)
{
}
}

While there is no problem with snippet 1, in case of snippet 2 the compiler
reports that the java.io.IOException is not thrown in the body of the
method. I could not understand the difference in this behaviour..
Further if the IOException is replaced with Exception then both the snippets are fine. Again this was not what I was expecting since Exception and all its subclasses (other than the RuntimeException branch) are checked
exceptions.
Any comments regarding the above 2 problems???
Thanks
 
Sirish Kumar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi All,
Sorry for posting the same question again. Sorry for the slip-up. I have got good replies to the same question posted earlier. Please do continue in the old thread and do not post replies to this one
Thanks a bunch
Compile Time error
 
    Bookmark Topic Watch Topic
  • New Topic