1

Best way to Handle Exceptions in C# Catch Block.I have no other choice but to Log the error to SQL DB in Catch block.Howver i am wondering what is the best way to catch exception if caught in Catch block itself?

Raidri
  • 17,258
  • 9
  • 62
  • 65
Sandeep
  • 615
  • 6
  • 13
  • try { throw; } catch(Exception ex) { //Log error to DB Whats best can be done here if error happens while logging to DB } – Sandeep Aug 23 '12 at 23:17
  • It depends. What do you *want* to happen if the logging throws an exception? – asawyer Aug 23 '12 at 23:23

1 Answers1

0

I would create a separate class to handle error reporting, and expose a function to handle logging the error in the DB.
I found this guide useful:

http://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET

Some code that I have used in the past looks like:

try{
    throw;
}
catch(Exception ex){
    LoggingClass.LogError(some paramaters,ex.tostring());
}

and then your logging class could look like

public static class LoggingClass {

    public static void LogError(some paramaters, ex.tostring()){

        //try to log to database


        //catch and report error in some other way
    }

}

I used this article as a reference in the past because I liked the idea of logging to a text file (in the event of a DB error) and then displaying a "nice" message to the user.

C# best practice error handling and passing error messages

Community
  • 1
  • 1
will
  • 312
  • 3
  • 9
  • Thanks Will...for looking into it so essentially there is no end:) but we can make it like 99% fail-safe thanks for your suggestion.i really appreciate you taking out time and responding to it – Sandeep Aug 24 '12 at 20:01