I want to write some try and catch that catch any type or exception, is this code is enough (that’s the way to do in Java)?
try {
code....
}
catch (Exception ex){}
Or should it be
try {
code....
}
catch {}
?
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.
Both approaches will catch all exceptions. There is no significant difference between your two code examples except that the first will generate a compiler warning because
exis declared but not used.But note that some exceptions are special and will be rethrown automatically.
http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception.aspx
As mentioned in the comments, it is usually a very bad idea to catch and ignore all exceptions. Usually you want to do one of the following instead:
Catch and ignore a specific exception that you know is not fatal.
Catch and log all exceptions.
Catch all exceptions, do some cleanup, then rethrow the exception.
Note that in the last case the exception is rethrown using
throw;and notthrow ex;.