Click here to Skip to main content
6,594,932 members and growing! (15,738 online)
Email Password   helpLost your password?
General Programming » Exception Handling » General     Intermediate

Simplest way of Exception Handling specially for database applications

By farukh ali farooq

Simplest way of exception handling, specially for database applications.
C#, SQL.NET 1.0, .NET 1.1, Win2KSQL 2000, VS.NET2003, DBA, Dev
Posted:18 Jan 2005
Views:36,203
Bookmarked:22 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 1.86 Rating: 1.95 out of 5
6 votes, 66.7%
1

2

3

4
3 votes, 33.3%
5

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


Member

Location: Pakistan Pakistan

Other popular Exception Handling articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
Generalodbcexception... Pinmemberfuhaizah18:48 23 Mar '06  
GeneralSometimes a generic approach isn't suitable. PinsupporterCharlie Williams7:10 18 Jan '05  
GeneralRe: Sometimes a generic approach isn't suitable. Pinmemberfarukh ali farooq19:01 18 Jan '05  
GeneralRe: Sometimes a generic approach isn't suitable. Pinmemberextremeg10:19 2 Aug '06  
General[Message Removed] Pinmemberimmetoz10:35 1 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Jan 2005
Editor: Rinish Biju
Copyright 2005 by farukh ali farooq
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project