Some Possible Combinations of try catch finally /Chapter 8 / Exception Handling

try catch finally


Some possible combinations of try catch finally


try 
{

}
catch(Exception e)
{

}

The above combination is valid.

—————————————————————————————————————————————
try
{

}
catch(ArithmeticException e)
{

}
catch(Exception e)
{

}

The above combination is valid.

—————————————————————————————————————————————
try
{

}
catch(ArithmeticException e)
{

}
catch(ArithmeticException e)
{

}

The above combination is invalid.And we will get Compile Time Error.

—————————————————————————————————————————————
try
{

}
catch(ArithmeticException e)
{

}
finally
{

}

The above combination is valid.

—————————————————————————————————————————————
try
{

}
finally
{

}

The above combination is valid.

—————————————————————————————————————————————
try
{

}
catch(ArithmeticException e)
{

}
try
{

}
catch(SQLException e)
{

}

The above combination is valid.

—————————————————————————————————————————————
try
{

}
catch(ArithmeticException e)
{

}
try
{

}
finally
{

}

The above combination is valid.

—————————————————————————————————————————————
try
{

}

The above combination is invalid.And we will get Compile Time Error.

—————————————————————————————————————————————
catch(Exception e)
{

}

The above combination is invalid.And we will get Compile Time Error.

—————————————————————————————————————————————
finally
{

}

The above combination is invalid.And we will get Compile Time Error.

—————————————————————————————————————————————
try
{

}
finally
{

}
catch(Exception e)
{

}

The above combination is invalid.And we will get Compile Time Error.

—————————————————————————————————————————————
try
{
}
System.out.println("javabychetan");
catch(Exception e)
{

}

The above combination is invalid.And we will get Compile Time Error.

—————————————————————————————————————————————
try
{
}
catch(ArithmeticException e)
{

}
System.out.println("javabychetan");
catch(Exception e)
{

}

The above combination is invalid.And we will get Compile Time Error.

—————————————————————————————————————————————
try
{

}
catch(Exception e)
{

}
System.out.println("javabychetan");
finally
{

}

The above combination is invalid.And we will get Compile Time Error.

—————————————————————————————————————————————
try
{
try
{
}
catch(Exception e)
{
}
catch(Exception e)
{
}

The above combination is valid.

—————————————————————————————————————————————
try
{
try
{
}

catch(Exception e)
{

}

The above combination is invalid.And we will get Compile Time Error saying "try without catch or finally"

—————————————————————————————————————————————

Conclusions:-



  1. In try catch finally, the order is very important. try catch finally

  2. Whenever we are using try block compulsory we should take either catch block or finally block, otherwise we will get compile time error saying "try without the catch or finally".

  3. Whenever we are using catch block compulsory try block must be required.Otherwise, we will get compile time error.

  4. Whenever we are using finally block compulsory try block must be required.Otherwise, we will get compile time error.

  5. Inside try catch and finally block, we can declare try, catch & finally blocks means nesting of try -catch-finally blocks is possible.

  6. For all the three try, catch, finally block { } curly braces are mandatory.

No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com