Global Exception Handler

M

Matt

I have written a console application that runs as a batch job on Windows
2000 Server. Every once in a while I am getting an exception thrown to the
console. Is there a way I can create a global handler that can catch this
exception and prevent the application from locking up? I am performing try
catch statements at all database and file write calls so I am unsure where
it is coming from.

Any suggestions or recommendations are greatly appreciated.

Thanks,

Matt
 
I

Ignacio Machin \( .NET/ C# MVP \)

HI,

What does the exception says?

Try to use AppDomain.UnHandledException and/or AppDomain.ThreadException

cheers,
 
M

Matt

Ignacio,

Actually I already have an event handler set up like that. The message is JIT Debugging Failed with the following error: 0x800405a6.

Documentation says:
JIT Debugging failed with the following error: 0x800405a6This error occurs if you attempt to do Just-In-Time debugging on a machine where there is no user logged onto the console. As a result, there is no session in which to display the Just-In-Time debugging dialog box.

To fix this problem, log onto the machine.

I am running this application under the scheduler so I am unsure why I get this with this application but yet none of my other .Net applications get this.


In main() I have:

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new EventHandler(LastChanceHandler);

Then the method:

void LastChanceHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception ex = ((Exception)(args.ExceptionObject));
Logging.WriteLine("****** LastChanceHandler ******");
Logging.WriteLine("ExceptionType: {0}", ex.GetType().Name);
Logging.WriteLine("HelpLine: {0}", ex.HelpLink);
Logging.WriteLine("Message: {0}", ex.Message);
Logging.WriteLine("Source: {0}", ex.Source);
Logging.WriteLine("StackTrace: {0}", ex.StackTrace);
Logging.WriteLine("TargetSite: {0}", ex.TargetSite);
string indent = "" + Microsoft.VisualBasic.Chr(9) + "";
Exception ie = ex;
while (!((ie.InnerException == null))) {
ie = ie.InnerException;
Logging.WriteLine(indent + "****** Inner Exception ******");
Logging.WriteLine(indent + "ExceptionType: {0}", ie.GetType().Name);
Logging.WriteLine(indent + "HelpLine: {0}", ie.HelpLink);
Logging.WriteLine(indent + "Message: {0}", ie.Message);
Logging.WriteLine(indent + "Source: {0}", ie.Source);
Logging.WriteLine(indent + "StackTrace: {0}", ie.StackTrace);
Logging.WriteLine(indent + "TargetSite: {0}", ie.TargetSite);
indent += "" + Microsoft.VisualBasic.Chr(9) + "";
}
}

Any ideas on how to capture what is wrong???

Thanks,

Matt
 
M

Matt

Bruce,

I have not run across that event before. Do have a sample or know where I
can get information on handling the thread level exception items inside my
application?

Thanks,

Matt
 
M

Matt

Bruce,

I thought the Application.ThreadException was only available in Forms
applications, this is a console application, or am I wrong.

Thanks,

Matt
 
B

Bruce Wood

You could be right... I handle both of them from both Console apps and
Forms apps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top