Hi all,
I am trying to catch all types of exceptions from a app regardless of
whether it is in debugger mode( VS development environment) or run the.exe
file outside the IDE.
My App contains the thousand of classes and i do not want to use the Try
Catch block in each class.because it is logically similar to GOTO
statement.I want to prevent exception from being swallowed.
I use a startup project, so that i can show the MDI main application window
within a Try Catch block. This all works fine when i run the App from within
the IDE, but when i run outside of the IDE i.e. run the .exe file. it
doesn't seem to catch the exception???
After reading your's reply i use both Application.ThreadException and
AppDomain.CurrentDomain.UnhandledException event in order to capture all
types of exception both within IDE and outside the IDE.
Now please guide me whether it is good technique for central exception
handling or i need two startup classes to handle Exception.One for handling
Exception Within the IDE or one for outside the IDE.
here is my sample code for handling all types of exception within one
startup class.
try {
Application.ThreadException += new
ThreadExceptionEventHandler(App_ThreadException);
AppDomain.CurrentDomain.UnhandledException +=new
UnhandledExceptionEventHandler(App_UnhandledExcept ion);
Application.Run();
} catch(Exception ex) {
//write nice error reporting routine!
Environment.Exit(0);
}
private static void App_ThreadException(object sender,
ThreadExceptionEventArgs e) {
//show message Box
}
private static void App_UnhandledException(object
sender,UnhandledExceptionEventArgs e) {
//show message Box
}
Hope this makes sense.
Waiting for your reply.
thanks inadvance.
Regards
Noor
"Jorge L Matos [MCSD.NET]" <matos_jorge_NOSPAM_AT_hotmail.com> wrote in
message news:62**********************************@microsof t.com...
There's also another event that is fired from the current AppDomain called
UnhandledException - I recommend assigning a delegate to both the Thread
and
the AppDomain exception events to limit the chance of the exception not
getting handled.
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(eh.UnhandledExcepti onHandler);
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(eh.On ThreadException);
"David Anton" wrote:
In your startup method:
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(eh.On ThreadException);
Application.Run(new frmMain());
where eh is an instance of the class that will handle the 'unexpected
exceptions'.
internal void OnThreadException(object sender,
System.Threading.ThreadExceptionEventArgs t)
{
Exception e = t.Exception;
StackTrace st = new StackTrace(e);
.... retrieve the method stack for diagnostic purposes
... display a friendly message, etc.
}
David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter
and the Instant VB C# to VB.NET converter
"Noor" wrote:
> please tell the technique of centralize exception handling without try
> catch
> blocks in c#.
>
>
>
>