Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
As you know the basic method for handle error is try ... catch ... finally structure.

If we want to handle every error and save it into a file, we have to add one line to catch section for save error details.

The question is:
Is there any solution for override catch event?

In other words, consider try ... catch ... finally structure similar to other application events and override catch section, run our custom code next run catch section code.

Is it possible?
Posted

If you want to log every error, you might want to look at more advance tools like Log4Net.
 
Share this answer
 
Comments
kia.sos 26-Apr-11 11:10am    
Thank you for your answer
Can you get me a sample or a link for more detail?
Abhinav S 26-Apr-11 23:25pm    
Try searching for Log4net. You will get tons of information on the internet.
For log4Net:
How to use log4net[^]
Log4Net Tutorial in C# .net [^]

If needed, look here[^] for more.


For Microsoft Enterprise Library - Logging Application block:
Introduction to the Logging Application Block[^]
Getting started with the Logging Application Block[^]


While providing you the link, came across this discussion[^] that differentiates and tells the advantages of them in detail.
 
Share this answer
 
Comments
kia.sos 26-Apr-11 12:17pm    
I see them. Thank you very much. But it was not what i wanted. I want to handle every catch section in all project, even if programmer write try ... catch ... finally structure. I want to first run my override catch next run programmer catch codes. Is it possible? If yes, how?
What you want is not possible (as far as I know).

All you can do is trapping the uncought exceptions:
C#
static class Program
{
    [STAThread]
    static void Main()
    {
       ...
       //Exceptions from main thread
       Application.ThreadException += new ThreadExceptionEventHandler(UIThreadUnhandledException);
       Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
       //Exceptions from other threads
       AppDomain.CurrentDomain.UnhandledException +=
           new UnhandledExceptionEventHandler(OtherThreadUnhandledException);
       ...
    }

    // This function is called if an exception was raised from the main thread and was not handled.
    // Your program can still continue after such an error.
    private static void UIThreadUnhandledException(object sender, ThreadExceptionEventArgs t)
    {
        ...
    }
    // This function is called if an exception was raised from another thread and was not handled
    // Your program can't continue after such an error: the framework will close it.
    private static void OtherThreadUnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
       ...
    }
 
Share this answer
 
Comments
kia.sos 26-Apr-11 13:07pm    
Thank you.
But i want to catch all, even handle exceptions!
I want to run my override catch code everitime, in all project, even if programmer write her/his own catch section.
Is it possible?
See here http://msdn.microsoft.com/en-us/library/ms733025.aspx[^] where it states all exceptions are logged if trace level is set to error.

The only way I know of to handle your 'override catch problem' in code is to rethrow the error from the catch so that your global error handler can catch it.
However if you attach a debugger, you can set the debugger to break when any exception is thrown.
In VS2010, this option is located in Debug->Exceptions (ctrl-alt-e)

Edit:
kia.sos wrote:
In other words, consider try ... catch ... finally structure similar to other
application events and override catch section, run our custom code next run
catch section code.


AFAIK, there is no way to run your catch first since exception propagate via bubbling not tunneling.
You could try coding your own trace listener and 'handle' the exception when the tracer receives it. However, I do not know if the tracer or the user's catch statement would be the first to be executed.
 
Share this answer
 
v2
Comments
kia.sos 27-Apr-11 2:40am    
Some times in our codes we use try ... catch structure. Fllowed catch we write other code that must be to running.
If i use rethrow in the catch section, it's cause not running my remaining code that writed after catch section.
I don't want it.
tnd2000 27-Apr-11 12:34pm    
Is this a program requirement or did you want to do it to debug some issues?
Exception can only be caught one time only. Outer exception will only catch if inner exception doesn't handle it, or if another exception occurs while it's being handled.
try
{
try
{
dosomething
}
catch
{
innerchach;
}
catch
{
this code will only execute if an exception is thrown in inner catch
}
}
kia.sos 27-Apr-11 2:43am    
Also if i set the debugger to break when any exception is thrown, it cause to don't running my remaining codes that writed after catch section.
tnd2000 27-Apr-11 12:26pm    
Debugger break when exception is thrown works like a break point. It just breaks execution, it does not kill the error handling codes.

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