catching Exceptions at a top level

  • Thread starter Thread starter Laxmikant Rashinkar
  • Start date Start date
L

Laxmikant Rashinkar

Hi,

I am working with a program that someone else wrote.
This program is full of bugs and unhandled exceptions are thrown all over
the code.
Instead of trying to catch the exception at each point it is thrown, I am
wondering
if it is possible to catch all exceptions at a top level. I tried the
following but it does not work.
Does anyone have any ideas about how this can be achieved?

thanks a lot
LK

-----

class Form1 : System.Windows.Forms.Form
{

[STAThread]
static void Main()
{
try
{
Application.Run(new Form1());
}
catch(Exception ex)
{
// unfortunately this does not catch any exceptions for me
}
}

}
 
Laxmikant Rashinkar said:
I am working with a program that someone else wrote.
This program is full of bugs and unhandled exceptions are thrown all over
the code.
Instead of trying to catch the exception at each point it is thrown, I am
wondering
if it is possible to catch all exceptions at a top level. I tried the
following but it does not work.
Does anyone have any ideas about how this can be achieved?

You have to hook the global exception even on the main thread.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
Laxmikant,

You will probably want to get the current AppDomain (through the static
CurrentDomain property on the AppDomain class) and then attach to the
UnhandledException event.

Hope this helps.
 
Just to add to this, it probably isn't the greatest idea to swallow these
exceptions. They've been thrown for a reason. Pretending they don't exist
will quite possibly leave the application in a worse state than just
exiting.

Nicholas Paldino said:
Laxmikant,

You will probably want to get the current AppDomain (through the static
CurrentDomain property on the AppDomain class) and then attach to the
UnhandledException event.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Laxmikant Rashinkar said:
Hi,

I am working with a program that someone else wrote.
This program is full of bugs and unhandled exceptions are thrown all over
the code.
Instead of trying to catch the exception at each point it is thrown, I am
wondering
if it is possible to catch all exceptions at a top level. I tried the
following but it does not work.
Does anyone have any ideas about how this can be achieved?

thanks a lot
LK

-----

class Form1 : System.Windows.Forms.Form
{

[STAThread]
static void Main()
{
try
{
Application.Run(new Form1());
}
catch(Exception ex)
{
// unfortunately this does not catch any exceptions for me
}
}

}
 
Oh, I do plan to handle them. It is just that I want to do it in one place,
based on the exception type, instead of debugging all of this guys code.

I dont quite understand what you wrote about attaching an
UnhandledException event. Could you give me a few more details plz?
your help greatly appreciated.
thanks
LK

Sean Hederman said:
Just to add to this, it probably isn't the greatest idea to swallow these
exceptions. They've been thrown for a reason. Pretending they don't exist
will quite possibly leave the application in a worse state than just
exiting.

message news:%[email protected]...
Laxmikant,

You will probably want to get the current AppDomain (through the static
CurrentDomain property on the AppDomain class) and then attach to the
UnhandledException event.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Laxmikant Rashinkar said:
Hi,

I am working with a program that someone else wrote.
This program is full of bugs and unhandled exceptions are thrown all over
the code.
Instead of trying to catch the exception at each point it is thrown, I am
wondering
if it is possible to catch all exceptions at a top level. I tried the
following but it does not work.
Does anyone have any ideas about how this can be achieved?

thanks a lot
LK

-----

class Form1 : System.Windows.Forms.Form
{

[STAThread]
static void Main()
{
try
{
Application.Run(new Form1());
}
catch(Exception ex)
{
// unfortunately this does not catch any exceptions for me
}
}

}
 
Back
Top