Explain Try/catch block of exception handling.
- You can enclose the code in Try/Catch/Finally block.
- You can catch all exceptions in the catch block.
- The third part of this block is finally.
- It is executed irrespective of the fact that an exception has been raised.
try:- It identifies a block of code for which particular exceptions are activated.
- It is followed by one or more catch blocks.
- It can be a compound statement.
catch:- It catches an exception with an exception handler at the place in a program where you want to handle the problem.
- It indicates the catching of an exception.
- There are multiple Catch blocks permitted.
Syntax:try
{
//Statements
}
catch(ExceptionName e1)
{
//Error handling code
}
catch(ExceptionName e2)
{
//Error handling code
}
catch(ExceptionName eN)
{
//Error handling code
}
- It provides a way to handle some or all possible errors that may occur in a given block of code while still running code.