473,480 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

catching Exceptions at a top level

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
4 1682
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2965
by: Rolf | last post by:
I understand a compilation error occurs when a method that throws no exceptions is the only code in a try block. What I don't understnad is why I can specify the catching of an Exception for a...
6
2447
by: EAS | last post by:
Hi, I'm wondering if there is any way to keep a program running when it runs into an error (using the 'try' structure) and print the traceback to the screen? Thanks.
2
1743
by: Keith Bolton | last post by:
I am handling exceptions currently using try, except. Generally I don't handle specific exceptions and am catching all. Then if an exception occurs, I would like to capture that error string....
7
2311
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
12
6086
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls...
3
2109
by: Stu | last post by:
Hi, Is there any way to catch every exception raised in an application (in order to write it to a log file for example). Thanks in advance, Stu
7
6370
by: Derek Schuff | last post by:
I'm sorry if this is a FAQ or on an easily-accesible "RTFM" style page, but i couldnt find it. I have some code like this: for line in f: toks = line.split() try: if int(toks,16) ==...
5
2209
by: Simon Tamman | last post by:
I have an object named DisasterRecovery. The Ctor of this object is this: private DisasterRecovery() { Application.ThreadException+= new...
25
2343
by: JJ | last post by:
I only want to catch 404 errors at the application level (the rest are will be handled by the customerrors section of the web.config). How do I check for the error code in the Application_Error...
0
6915
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7097
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6750
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3003
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.