What is the best way to handle an exception?
Also, why should I never write:
catch (Exception ex) { throw ex; }
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.
The best way to handle an exception is to do something meaningful in the
catchblock (the one which in your example containsthrow ex). The definition of “meaningful” completely depends on your needs.You should not do
catch (Exception ex) { throw ex; }because that brakes the exception chain. It is perfectly fine to catch an exception, handle it and re-throw so that the calling code can see it, but you should be doing so like this: