Click here to Skip to main content
15,887,244 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:

Hi

I have an application that holds a lot of try and catch blocks which is good for error handling.

Now i need to add a function into the catch block that reports error to the database, i only need to put the function into the catch block

that is fine if you have a small application unlike mine.

Posted
Updated 24-Nov-09 23:05pm
v2

This is a repost of this question. Please do not repost; just edit your original question.
 
Share this answer
 

For normal exception create a general function which logs any exception into the database like 

public void LogException(Exception ex)
{
//Store ex.Message / ex.StackTrace and whatever else into database 
}



Now from catch block call this function to log.

catch(Exception ex)
 LogException(ex);


You can also check the Type of exception inside LogException function to log specific values for an exception.

 
Share this answer
 
It's likely that the PostSharp is a good choice,use attribute to log exceptions
 
Share this answer
 

You needed to handle this from the start, it's a bit late now. You're going to have to go back and change all your methods to call your database method. That's not so hard to do, a global search and replace would do it, replace

catch(Exception e)

{

with

catch(Exception e)

{

     DataLayer.LogException(e);

 
Share this answer
 
See my code.catch (Exception EXApplication) { ExceptionHandler.Publish(EXApplication); } Where ExceptionHandler.Publish is my library function to log error in database
 
Share this answer
 
v2
Like others have suggested, I suggest you just bite the bullet and do a find and replace. Note that Visual Studio has support for regular expression searches, so you can find any catch block (no matter the exception caught). However, if you really want to look into this further, you might be able to use runtime code generation and reflection to find catch blocks then inject code into them at runtime. Here's an article to get you started (although I strongly advise against going down this path, as it could get quite complex and may not yield results): .Net Code Injection. Here is a CP article on Reflection.Emit(): Article.
 
Share this answer
 

ok but i am really stuck into this issue,

i know i can add a global error hangling like

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

but this will only works for unhandled excetions but what about handled exceptions how to add new event handlers

 
Share this answer
 

this will work if you have a small amount of try catch blocks but in my case doing this will take alot of time

any others suggestions

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900