Click here to Skip to main content
Licence 
First Posted 18 Jan 2005
Views 47,857
Bookmarked 27 times

Simplest way of Exception Handling specially for database applications

By | 18 Jan 2005 | Article
Simplest way of exception handling, specially for database applications.
 
Part of The SQL Zone sponsored by
See Also

Introduction

This article and the source provided demonstrate how you can handle exceptions in .NET applications in a generic way. This will ensure that there are no unhandled exceptions in your application thread.

Background

Exception handling is an important part of all applications. A few days ago, I was writing a database oriented application that consisted of over 100 forms. During the design of my application, I got different ways of exception handling, but actually I was in search of a way that was the shortest and one that requires minimum lines of code. For this project, I wrote about 10 to 12 lines of code.

Using the code

For handling exceptions on application thread level, I handle the application's exception event as follows:

static void Main() 
{ 
  Application.ThreadException+= new 
     System.Threading.ThreadExceptionEventHandler(Application_ThreadException); 
  Application.Run(new testExceptionHandler()); 
}

Now, whenever there is any exception that occurs in an application thread, this event will be called.

As I have already stated, it was a database application, now there was a challenge of how I will identify specific exceptions like primary key violation etc..

For this, in the MDI form in that application, I declared a string variable that will later contain the active form name.

Fortunately I was using SqlClient that has a SqlException class, this will be thrown if there is any database related issue. I handle this as follows:

private static void Application_ThreadException(object sender, 
                         System.Threading.ThreadExceptionEventArgs e)
{
   try
   {
        if(e.Exception is SqlException)
        {
             SqlException ex = (SqlException)e.Exception; 
             if (ex.Number == 547)
                ErrorMessage("Record cannot be deleted or changed " + 
                               "as it is being used somewhere else");
    
             else if (ex.Number == 2627)
                ErrorMessage("Record cannot be saved, as another " + 
                             "record with this key already exists");
        
             else
                ErrorMessage(ex.Message.ToString());
        }
        else
             ErrorMessage("System Error :"+e.Exception.Message.ToString());

    }
    catch(Exception ex)
    {
        ErrorMessage("System Error: Reporting to log");
    }
}

sqlexception object contains a number attribute that identifies the type of sqlexception thrown.

I have tested this method in my application, it is running well. For a more descriptive message I have used the form name of the active form for intimating the user about the problem.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

farukh ali farooq



Pakistan Pakistan

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalodbcexception... Pinmemberfuhaizah17:48 23 Mar '06  
GeneralSometimes a generic approach isn't suitable. PinmemberCharlie Williams6:10 18 Jan '05  
GeneralRe: Sometimes a generic approach isn't suitable. Pinmemberfarukh ali farooq18:01 18 Jan '05  
GeneralRe: Sometimes a generic approach isn't suitable. Pinmemberextremeg9:19 2 Aug '06  
General[Message Removed] Pinmemberimmetoz9:35 1 Oct '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 18 Jan 2005
Article Copyright 2005 by farukh ali farooq
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid