In Java, is there any way to get (catch) all exceptions instead of catching the exceptions individually?
In Java, is there any way to get (catch) all exceptions instead of catching
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you want, you can add throws clauses to your methods. Then you don’t have to catch checked methods right away. That way, you can catch the
exceptionslater (perhaps at the same time as otherexceptions).The code looks like:
Then later you can deal with the
exceptionsif you don’t wanna deal with them in that method.To catch all exceptions some block of code may throw you can do: (This will also catch
Exceptionsyou wrote yourself)The reason that works is because
Exceptionis the base class for all exceptions. Thus any exception that may get thrown is anException(Uppercase ‘E’).If you want to handle your own exceptions first simply add a
catchblock before the generic Exception one.