I have some piece of code. In that there are chances to get many number of exceptions. My doubt is, to handle all those exceptions do i have to write catch blocks for each type of exception. Is it an efficient way or not. Except using throws keyword, If any other solutions are there please suggest me to do that. Any response will be appreciated.
Thanks in advance
It depends on what kind of exceptions you’re trying to catch. Everything that can be thrown implements Throwable, so you can catch everything with
including run time errors and all. As Amjad mentions, you can narrow that a little with
which just catches Exception and its subtypes.
The problem with both of these is that they catch too much; you can work around that but you risk catching an important problem and then not handling it.
If you have just a few different exceptions, you’re probably best off with an exception comb
You have one other option if these are your own exceptions: make a class hierarchy of your own exceptions
Now you can pick any subtree of classes, as with
which will catch both
MyExceptionSubtypeAandMyExceptionSubtypeASubsub1.