C# · Dotnet

Thought points on Exception Handling

Dear Reader,

Today i was having a discussion with one my team member about exception handling when i saw my team members code (No, i wasn’t reviewing his code or some thing).

This team member had written a business logic code (Core part i would say) where the exception was handled and just thrown as shown in below sample code at couple of places :

public void SomeCoreMethod()
{
 try
 {
   //blah blah blah code 1
   //blah blah blah code 2
   //blah blah blah code 3
   //blah blah blah code 4
 }
 catch(Exception ex)
 {
   throw ex;
 }
}

The above code is absolutely fine, but it has some corner spots which any developer can over look. So below were my findings with that person which started a bit of long discussion. So this post is about that conversation which might help you if you have such dilemma situations.

So my finding was presented to that developer as below (Yes based on my stackoverflow, Jeffrey Ritcher book, Jon skeet articles, etc. and my own thoughts references)

  1. Catching generic exception is never good option. This shows that you as a developer does not know about your own code. Referenced Amazed saint blog post to that developer as a proof read.
  2. Just catching and throwing is not at all useful. Because when a catch block is encountered, the CLR has to stop that thread execution, unwind its stack, save memory state, etc. which is very costly if its done every where.
  3. Throwing an exception object as above will re-initialize the StackTrace data where in you might loose the original source of exception occurrence. So hence just use throw
  4. Rather than handling here, just let the exception flow and let us handle it in the one place perhaps UI layer? Where we can log it and then have a graceful recovery. This helps in centralized logging and better recovery and handling mechanism.

To my above points, the developer continued further discussions perhaps presented self view points which i do respect completely though it is write or wrong.

So the view points of that developer was as below:

  1. Yes it can be changed but lets see.
  2. Ok! I shall not just throw rather log it here and then throw.
  3. Is it? Nah i do not think so. I am sure it can not be happening that way : Its a violation of feature integrity meaning same operation can not be done this way and the other way.
  4. To this one more member jumped in and argued that it need not to be done at one place, but rather do it there itself and throw to which the actual colleague did not oppose nor accepted.

So for the above points i had to explain to that colleague couple of my thoughts and why i think and agree along with experts mentioned above.

  • Logging at that place and re-throwing the same is not a bad idea. But think about the future. If you have to change the logging logic, you may have to find every where this logic and change it or if you have to remove the logic completely then again you have to do it every where.
  • To prove that throw and throw ex actually differ, had to write a sample app and prove. Also i stated my explanation about why its not data/implementation integrity as below:
    Integrity issue happens only when =

If you have an Action A which can be done/solved by only 1 solution S but the outcome of S differs on conditions C1, C2, etc. : Then its a feature integrity violation.
But in this case, the same action A can be done/solved by 2 solutions S1 and S2 where as their outcome is the same depending on the way its usage and needs : Then its not at all any violation but rather flexibility.

After presenting my thoughts as stated above, the discussion seem to end and that friend did not come back with any further discussion points. I hope my view points was constructively received.

So dear reader:

Thanks & Happy Coding,
Zen 🙂

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s