Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
Ravi Vishwakarma
24-Apr-2025Handling multiple exceptions in Java is common when a block of code might throw more than one type of exception. You can handle them using multiple
catch
blocks or a multi-catch block.1. Multiple
catch
BlocksYou can write one
catch
block for each exception type.Example:
Notes:
Exception
).Exception
first), the compiler will throw an error.2. Multi-Catch Block (Java 7+)
You can catch multiple exceptions in a single catch using
|
(pipe symbol).Example:
Multi-catch rules:
IOException | Exception
is invalid).e
) is effectively final – you can’t reassign it.Summary Table:
catch
blocksLet me know if you want to see this with custom exceptions too!