PC Review


Reply
Thread Tools Rate Thread

catching Exceptions at a top level

 
 
Laxmikant Rashinkar
Guest
Posts: n/a
 
      15th Feb 2005
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
}
}

}


 
Reply With Quote
 
 
 
 
Chad Z. Hower aka Kudzu
Guest
Posts: n/a
 
      15th Feb 2005
"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/
 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      15th Feb 2005
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 Removed)

"Laxmikant Rashinkar" <LK-at-televital-dot-com> wrote in message
news:%(E-Mail Removed)...
> 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
> }
> }
>
> }
>
>



 
Reply With Quote
 
Sean Hederman
Guest
Posts: n/a
 
      15th Feb 2005
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]" <(E-Mail Removed)> wrote in
message news:%(E-Mail Removed)...
> 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 Removed)
>
> "Laxmikant Rashinkar" <LK-at-televital-dot-com> wrote in message
> news:%(E-Mail Removed)...
>> 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
>> }
>> }
>>
>> }
>>
>>

>
>



 
Reply With Quote
 
Laxmikant Rashinkar
Guest
Posts: n/a
 
      15th Feb 2005
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" <(E-Mail Removed)> wrote in message
news:cutiq8$fkp$(E-Mail Removed)...
> 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]" <(E-Mail Removed)> wrote

in
> message news:%(E-Mail Removed)...
> > 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 Removed)
> >
> > "Laxmikant Rashinkar" <LK-at-televital-dot-com> wrote in message
> > news:%(E-Mail Removed)...
> >> 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
> >> }
> >> }
> >>
> >> }
> >>
> >>

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Catching exceptions Andy B. Microsoft C# .NET 3 16th Mar 2010 10:33 AM
Catching Exceptions Jeffrey Walton Microsoft C# .NET 5 25th Nov 2007 12:05 AM
catching exceptions bitshift Microsoft Dot NET Framework 4 31st Aug 2007 10:51 AM
Catching non-UI threads' exceptions. BLUE Microsoft C# .NET 1 26th May 2007 02:34 PM
Catching ALL Unhandled Exceptions James Hancock Microsoft Dot NET Framework 5 13th Jan 2004 04:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:04 PM.