Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle exceptions?

Tags:

c#

silverlight

It's a question about best .net practise. I always caught and stored information about exceptions in all of my web and window forms applications as follows:

  1. Surrounded each method with try catch(Exception exception)
  2. For any layer except front layer, threw exception to the layer above
  3. In the front layer, logged the exception to a log file (usually using log4config.dll) and presented a user friendly message to the user.

Is this the best way to handle exceptions? Or should I do something different in my next projects?

like image 923
InfoLearner Avatar asked Dec 12 '10 19:12

InfoLearner


People also ask

How do you handle exceptions in oops?

C++ exception handling is built upon three keywords: try, catch, and throw. throw − A program throws an exception when a problem shows up. This is done using a throw keyword. catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem.


1 Answers

I wouldn't add 1 & 2 unless I had some specific reason; for example to alter (wrap) the message; there is no need since exceptions will raise upwards anyway. And done incorrectly you can accidentally remove the all-important stack-trace (throw; vs throw ex; - the first being preferred).

like image 97
Marc Gravell Avatar answered Oct 14 '22 08:10

Marc Gravell