Wednesday, September 3, 2008

Don't Try To Handle What You Cannot Handle

One thing that came up last week was that the sorting on one of the paged grid did not work, ok…so we were hunting the bug down, but lucky something popped up in the log file and saved the day.
Clicking on the sort headers, makes a service call to retrieve the sorted data, and the UI code was written in such a way that the service call is trapped inside a try-catch block, and if any exception occurred, the exception is logged and empty data returned.


And unfortunately the service query had a bug on it. So which brings me to the point, you should catch exceptions only when you can handle it, for example, you might have anticipated that a DuplicateNameException would be thrown, well go ahead, catch it and show a nice message to the user, but if you cant, don’t put it inside a catch block and bypass the exception just for the sake of not crashing the UI.
If you really want to log it, fine, log it then remember to re-throw the exception although re-throwing as its own performance cost.


And to be amazed, the error was there al the time and no one noticed it, which brings to the justifiation the if you dont handel exceptions that you cant handle, you would have seen the issue so much earlier.

No comments:

Post a Comment