LECTURE -60: PROGRAMMING IN JAVA - MULTIPLE CATCH BLOCKS, MULTIPLE CATCH SYNTAX- BCA- S3.

Опубликовано: 21 Май 2026
на канале: BOSCOCAMPUSVISION
86
2

Multiple catch blocks
A try block can be followed by multiple catch blocks. It means we can have any number of catch blocks after a single try block. If an exception occurs in the guarded code(try block) the exception is passed to the first catch block in the list. If the exception type matches with the first catch block it gets caught, if not the exception is passed down to the next catch block. This continue until the exception is caught or falls through all catches.
Multiple Catch Syntax
To declare the multiple catch handler, we can use the following syntax.

try
{
// stmt code
}
catch(Exception1 e)
{
// stmt code
}
catch(Exception2 e)
{
// stmt code
}
finally clause
A finally keyword is used to create a block of code that follows a try block. A finally block of code is always executed whether an exception has occurred or not. Using a finally block, it lets you run any cleanup type statements that you want to execute, no matter what happens in the protected code. A finally block appears at the end of catch block.
User-defined Custom Exception in Java
Java provides us facility to create our own exceptions which are basically derived classes of Exception.