By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,020 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,020 IT Pros & Developers. It's quick & easy.

catching Exceptions at a top level

P: n/a
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
}
}

}
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Laxmikant Rashinkar" <LK-at-televital-dot-com> wrote in news:#6tS304EFHA.392
@TK2MSFTNGP14.phx.gbl:
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/
Nov 16 '05 #2

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Laxmikant Rashinkar" <LK-at-televital-dot-com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
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
}
}

}

Nov 16 '05 #3

P: n/a
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2***************@TK2MSFTNGP15.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Laxmikant Rashinkar" <LK-at-televital-dot-com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
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
}
}

}


Nov 16 '05 #4

P: n/a
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" <us***@blogentry.com> wrote in message
news:cu**********@ctb-nnrp2.saix.net...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:%2***************@TK2MSFTNGP15.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Laxmikant Rashinkar" <LK-at-televital-dot-com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
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
}
}

}



Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.