Can we write multiple catch blocks under single try block? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

Can we write multiple catch blocks under single try block?

Multiple catch blocks

18th May 2019, 9:17 PM
MD Salauddin
MD Salauddin - avatar
2 Answers
+ 3
Yes we can write multiple catch blocks under a single try block. But when we use multiple catch statements, it is important that exception subclasses come before any of their superclass. Ans the reason is a catch statement with super class will catch exception of that type plus any of its subclass. The catch statement with sub class exception is a unreachable code which gives error in Java program. If the order of catch blocks from subclass to parent class is maintained then program will execute normally.
19th May 2019, 9:21 AM
Vaishnavi Rajkumar Pawar
- 1
Yes, but specific exceptions need to be written before the general ones
19th May 2019, 4:32 AM
Hothouseinwar Polik
Hothouseinwar Polik - avatar