Menu
Topics Index
...
`


Exceptions >
Siva Nookala - 03 Mar 2017
Below is the summary of various points we need to know about exception handling.

We need to note the following points about exception handling.
  • Every try should have a catch or finally block. We can not have a catch or finally block with out the try block.
// Valid - Try With Catch
try
{
...
}
catch(Exception e)
{
}
// Valid - Try With Catch And Finally
try
{
...
}
catch(Exception e)
{
...
}
finally
{
...
}
// Valid - Try With Only Finally
try
{
...
}
finally
{
...
}
// Invalid - No Try Block
catch(Exception e)
{
...
}
finally
{
...
}
  • A try can have multiple catch blocks but it can have only one finally block.
// Valid - try with multiple catch blocks and only one finally
try
{
}
catch(SubSubClassException e)
{
...
}
catch(SubClassException e)
{
...
}
catch(Exception e)
{
...
}
finally
{
...
}
// INVALID - try with multiple finally blocks
try
{
}
catch(Exception e)
{
...
}
finally
{
...
}
finally
{
...
}
  • When we have multiple catch blocks for a try block, the sub-classes must be caught first and then the super-classes.
class AException extends Exception
{
...
}
class B1Exception extends AException
{
...
}
class B2Exception extends AException
{
...
}
// Valid - Since the sub-sub-classes B1Exception, B2Exception are before the sub-class AException which is before the super-class Exception in the catch blocks
try
{
}
catch(B1Exception b1)
{
}
catch(B2Exception b2)
{
}
catch(AException a)
{
}
catch(Exception e)
{
}
// INVALID - Since the sub-classes AException is before sub-sub-class B2Exception.
try
{
}
catch(AException a)
{
}
catch(B2Exception b2)
{
}
catch(Exception e)
{
}
// Valid - Since the sub-class AException is before super-class Exception. Although it does not include the sub-sub-classes.
try
{
}
catch(AException a)
{
}
catch(Exception e)
{
}
// Valid - Since the sub-sub-class B1Exception is before super-class Exception. Although it does not include the sub-class AException.
try
{
}
catch(B1Exception b1)
{
}
catch(Exception e)
{
}
  • try blocks could be nested. We can have try blocks inside a catch block or inside a finally block.
try
{
    try
    {
        try{...}catch(Exception e){...}
    }
    catch(Exception e)
    {
        try{...}finally{...}
    }
}
catch(Exception e)
{
    try{...}catch(Exception e){...}finally{ }
}
finally
{
    try{...}catch(Exception e){...}
}
  • throw keyword can be used to throw a new exception or re-throw a caught exception.
try
{
    // Raise a new Exception
    if(...) throw new AException();

}
catch(B1Exception b1)
{
    // Rethrow the caught exception after doing any required handling
    throw b1;
}
  • In a method body, if we are throwing an exception, then that exception type (class name) should be included in the throws clause of the method. If there are more than one type of exception being thrown, then all the exceptions should be included or a common super-class of these exceptions can be included.
// Valid - since what ever exceptions being thrown are included in the throws clause
void method() throws B1Exception, B2Exception
{
    if(...) throw new B1Exception;
    
    if(...) throw new B2Exception;
}
// Valid - since AException which is the super-class of B1Exception and B2Exception is included in the throws clause
void method() throws AException
{
    if(...) throw new B1Exception;
    
    if(...) throw new B2Exception;
}
// INVALID - since the exceptions thrown are not included in the throws clause
void method()
{
    if(...) throw new B1Exception;
    if(...) throw new B2Exception;
}
  • If a method throws a unchecked exception or RuntimeException (or any class which extends from RuntimeException), then it is not necessary to include the calling of that method in a try block. But if it is a checked exception we need to include the calling of the method in try block and implement the exception handling in the catch block.
class YException extends RuntimeException
{
...
}
class Z1Exception extends YException
{
...
}
class Z2Exception extends YException
{
...
}
// Valid - since Z1Exception and Z2Exception are run-time exceptions, they need not be included in the throws clause of the method
void method()
{
    if(...) throw new Z1Exception
    if(...) throw new Z2Exception
}
// Valid - since Z1Exception and Z2Exception are run-time exceptions, there is no problem even if we include them in the throws clause of the method
void method() throws Z1Exception, Z2Excetpion
{
    if(...) throw new Z1Exception
    if(...) throw new Z2Exception
}

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App