.NET Daily

C#

C# Coding Standards Series – Exception Handling

Posted on .

C# Coding Standards Series – Exception Handling

Introduction

This post is part of a series that covers most of notable C# coding standards that you should follow.

You can download the document with all of the post from here: [download id=”2″ format=”1″]

You can see here the other posts within the series.


As programmers we always want to write code of the highest quality. Unfortunate we are not perfect so exceptions appear as a side effect of our code. Knowing that, we must not punish the user of our product to see our mistakes. Please take a look at the following basic rules about exception handling.

1 General Rules

1.1 Only catch exceptions that you can handle;
1.2 Avoid nesting try/catch within catch block;
1.3 Use finally block only for releasing used resources;
1.4 Always use validation to prevent the exception:

Ex:

Good:

if (status!=null && status.IsCompleted == true)
{
   //some code here
}

Bad:

if (status.IsCompleted == true)
{
   //some code here
}

1.5 Always add the [Serialize] attribute to exception classes;
1.6 Avoid re-throwing an exception;
1.7 Do not show important exception information to the client;

Please share your opinion about this post by commenting below.

Darius

Darius

Darius Dumitrescu is a creative Senior CMS Consultant with in depth .NET knowledge, focused on Web Development and Architecture Design.

Comments
  • user

    AUTHOR paguru

    Posted on 4:55 am September 1, 2011.
    Reply

    This article is completely useless.

    • user

      AUTHOR darius

      Posted on 7:47 am September 1, 2011.
      Reply

      I admit that this section needs more data but since this article is a work in progress, you can add if you want better exception handling rules.

  • This site uses Akismet to reduce spam. Learn how your comment data is processed.

    View Comments (0) ...
    Navigation