Windows Develop Bookmark and Share   
 index > Windows Forms General > Advice on Exception Handling
 

Advice on Exception Handling

Would appreciate any advice on this please?

Say I have created a .NET class library to log exceptions to a database (ErrorLoggingDLL).

I reference that DLL in another .NET Windows application.

I then write a method in the latter along the lines of : -

public void test()

{

try

{

DoSomething();

}

catch (Exception ex)

{

ErrorLoggingDLL.LogError(ex)

}

}

What should my strategy be if the LogError method itself throws an exception? Should I just ignore it or replace the code above with....

public void test()

{

try

{

DoSomething();

}

catch (Exception ex)

{

try

{

ErrorLoggingDLL.LogError(ex)

}

catch (Exception)

{

// what should I do here - because my ErrorLoggingDLL.LogError itself has thrown an error.

// should I a) not throw an error from the ErrorLoggingDLL.LogError method?

// b) throw it and handle it here (if so what do I do with the error)?

// c) throw it and handle it here by the deprecated practice of a catch block with no code in it?

}

}

}

PremjitM  Friday, March 07, 2008 2:42 PM

Well this question comes up quite often. There is no right or wrong practice, I believe, it all depends on personal preference and/or the criticality of exception you are trying to log into the database.

Lets say you tried to log a major critical error in the database but you were not able to create the connection for whatever reason you should (in my opinion) have fall back strategy in place i.e.

#1 Log that error to the event log so you or any other fellow developeror your end user can see it and figure out what went wrong.

#2 Log it in a custom log file.

#9 Have the error bubble up and display it to the user so user can report exactly what went wrong.

...

and so on, you get the idea.

Then there is almost always a follow-up question ... what if the event log routine throws an exception, hencestarting the wild goose chase again Smile

In any event at least let the user know that something went wrong if it can be logged then log if not just display it on the screen so the user may have some reference when they contact support.

Hope that helps.
DotNet_Student  Friday, March 07, 2008 7:41 PM
Hi,

I think the best choise here is to not throw an error from the ErrorLoggingDLL.LogError method, because it not a good practise to have some code in catch block whick can also throw an error.
Mykhaylo Terentyak  Friday, March 07, 2008 3:54 PM
I would agreee. The logging function should do all its work in a try/catch block, then you can handle the exception in the library (by writing the error to the event log or something else appropriate).

Michael Bird from Sharp Dudes  Friday, March 07, 2008 6:56 PM

Well this question comes up quite often. There is no right or wrong practice, I believe, it all depends on personal preference and/or the criticality of exception you are trying to log into the database.

Lets say you tried to log a major critical error in the database but you were not able to create the connection for whatever reason you should (in my opinion) have fall back strategy in place i.e.

#1 Log that error to the event log so you or any other fellow developeror your end user can see it and figure out what went wrong.

#2 Log it in a custom log file.

#9 Have the error bubble up and display it to the user so user can report exactly what went wrong.

...

and so on, you get the idea.

Then there is almost always a follow-up question ... what if the event log routine throws an exception, hencestarting the wild goose chase again Smile

In any event at least let the user know that something went wrong if it can be logged then log if not just display it on the screen so the user may have some reference when they contact support.

Hope that helps.
DotNet_Student  Friday, March 07, 2008 7:41 PM
Many thanks to all for your advice; much appreciated.

PremjitM  Saturday, March 08, 2008 7:45 AM

You can use google to search for other answers

Custom Search

More Threads

• unselecting checkedlistbox and listbox items
• Copy file from Project to D Drive
• Best user interface control suite?
• Real-Time Communication between Forms
• Value Close() cannot be called while doing CreateHandle()
• An object reference is required.... error, please help
• Catch missing ActiveX control error
• Problem to Resizing RichTextBox control when paste the text
• crical error message
• How do you take text from a rich text box and put it into your code?