473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

creating a base exception for a windows application

I am writing a windows C# application, and I want to ensure that all
exceptions, handled or otherwise, are logged to the local event log.
I can do this for the handled exceptions, but I am not sure how to
override (?) the system exception such that un-handled exceptions are
logged.
I know that you can do this in delphi by pointing the application
exception to your own base exception handler. Is there any similar way
that this can be done in the dot.net V2 framework?

Many thanks in advance

Oct 10 '06 #1
3 1896
I usually create a class, called
EntryPoint.cs
[STAThread]
static void Main(string[] args)
{
try
{
Application.Run(new Form1()); //Whatever your startup form is
}
}
catch (Exception ex)
{
//Notify User
MessageBox.Show( "A (uncaught) exception has occured. MyApp
cannot continue." );
//Shut down application
Application.Exit( );
}
}
This will catch any uncaught exceptions.


You should should google
try catch finally brad abrams
and you can read where
"You should be writing many many more
try/finally
blocks
and not so many
try/catch/finally
blocks.


You should also check out
http://www.microsoft.com/downloads/d...B-8F22CA173E02
or
http://msdn.microsoft.com/library/de.../html/ehab.asp

Its default behavior is to write to the EventLog, but you can EXPAND this if
you need to.

I wrote my own custom publisher which allows any/all of 3 things:

Write a .log file on users local machine.
Send an email.
Update a db with insertion.
While the EMAB (Exception Management Application Block) is older, its still
good stuff.

The EMAB basically requires some basic app.config setup. and then you add
ONE line of code.

altered from above

catch (Exception ex)
{

ExceptionManager.Publish(ex); // Yes, this is it. Even for my
..logfile, emailer, db update, this is all I put in my code. I set up
everything in app.config.

//Notify User
MessageBox.Show( "A (uncaught) exception has occured. MyApp
cannot continue." );
//Shut down application
Application.Exit( );
}



"CharlieC" <go****@ccurtis.comwrote in message
news:11*********************@c28g2000cwb.googlegro ups.com...
I am writing a windows C# application, and I want to ensure that all
exceptions, handled or otherwise, are logged to the local event log.
I can do this for the handled exceptions, but I am not sure how to
override (?) the system exception such that un-handled exceptions are
logged.
I know that you can do this in delphi by pointing the application
exception to your own base exception handler. Is there any similar way
that this can be done in the dot.net V2 framework?

Many thanks in advance

Oct 10 '06 #2

CharlieC wrote:
I am writing a windows C# application, and I want to ensure that all
exceptions, handled or otherwise, are logged to the local event log.
I can do this for the handled exceptions, but I am not sure how to
override (?) the system exception such that un-handled exceptions are
logged.
I know that you can do this in delphi by pointing the application
exception to your own base exception handler. Is there any similar way
that this can be done in the dot.net V2 framework?

Many thanks in advance
You want to handle two events: AppDomain.UnhandledException, and
Application.ThreadException. See the following article:

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

and the relevant MSDN help for these two events.

However, you don't want to log handled (caught) exceptions. There are
several reasons to catch an exception, but in every case it means that
the application has successfully dealt with the problem and is moving
on. Logging caught exceptions is like having your app write log lines
saying, "I'm still working OK..." "I'm still working OK..." ad nauseum.

If you're catching exceptions that really mean that the application is
sick and is going to shut down, then you really shouldn't be (unless
you have to do something special before the application shuts down...
and even then the above two event handlers are likely better places to
do that).

Oct 10 '06 #3
Charlie,
Here is a simplified example that may help:

[STAThread]
static void Main()
{

Application.ThreadException += new
ThreadExceptionEventHandler(OnThreadException);
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_Unhan dledException);

Application.EnableVisualStyles();
Application.Run(new Form1());
}

// Handles the exception event for all other threads
static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{

LogException(e.ExceptionObject);
}

// Handles the exception event from a UI thread.
static void OnThreadException(object sender,
ThreadExceptionEventArgs t)
{

LogException(t.Exception);
}
private LogException (Exception e)
{
// your cool logging code here
}

--Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"CharlieC" wrote:
I am writing a windows C# application, and I want to ensure that all
exceptions, handled or otherwise, are logged to the local event log.
I can do this for the handled exceptions, but I am not sure how to
override (?) the system exception such that un-handled exceptions are
logged.
I know that you can do this in delphi by pointing the application
exception to your own base exception handler. Is there any similar way
that this can be done in the dot.net V2 framework?

Many thanks in advance

Oct 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Matt Warner | last post by:
Hi guys, A couple of people have already posted questions about similar issues but haven't had any response. Occasionally, sometimes after running the app for a few hours, it bombs out saying...
6
by: DraguVaso | last post by:
Hi, In my application, on some given actions while debugging in Visual Studio, I suddenly get a "System.ComponentModel.Win32Exception was unhandled" Message="Error creating window handle."...
2
by: Jon Davis | last post by:
I have a full-blown application that consists of several (fifteen or so) assembly DLLs, each being a separate VS.NET project that outputs to the main DLL's bin directory. They are all strongly...
2
by: Technical Group | last post by:
Friends, Can anybody help me out by sending a piece of C# code showing how to add an active directory user to a particular user group? If the group does not exist, then create it. Thanks in...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
2
by: superseed | last post by:
Hi, I'm pretty new to C#, and I'm quite stuck on the following problem. I would like to add to my application a Windows.Form (singleton) on which I could display a message of one of the...
3
by: some one | last post by:
I have kind of wired problem, I using httpwebrequest to post form data to server , in the GetResponse stage a WebException occurred, after tracing the actual error that occurs on the server, I...
0
by: Vimal Thakkar | last post by:
Hi, I have created one console base application which uses third party control FREDI. FREDI provides COM Control FREDI.TLB to be used with .NET. When i am adding reference of FREDI.TLB in my...
5
by: Vibhesh | last post by:
I am facing problem with TimeSpan structure when DirectX is used. Following is the sample code that causes the problem: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
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...

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.