473,586 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1908
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.Exi t( );
}
}
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)
{

ExceptionManage r.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.Exi t( );
}



"CharlieC" <go****@ccurtis .comwrote in message
news:11******** *************@c 28g2000cwb.goog legroups.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.Unhan dledException, and
Application.Thr eadException. 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.Thr eadException += new
ThreadException EventHandler(On ThreadException );
AppDomain.Curre ntDomain.Unhand ledException += new
UnhandledExcept ionEventHandler (CurrentDomain_ UnhandledExcept ion);

Application.Ena bleVisualStyles ();
Application.Run (new Form1());
}

// Handles the exception event for all other threads
static void CurrentDomain_U nhandledExcepti on(object sender,
UnhandledExcept ionEventArgs e)
{

LogException(e. ExceptionObject );
}

// Handles the exception event from a UI thread.
static void OnThreadExcepti on(object sender,
ThreadException EventArgs 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
600
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 that it could not create a windows handle. On one machine the stacktrace is this:
6
3238
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." exception. The problem is that this exception isn't raised somewhere in a method, so it just shows up, and it causes the application to shut down. ...
2
1560
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 named. I have registered the main DLL, which references the other DLLs, to the GAC cache. I have built a plug-in for a third party application with...
2
6602
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 advance -Hari
15
6735
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 javascript or vbscript it works. I will appreciate any help. I do the following (C#):
2
2650
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 following type : Exception, Error, Warning, Infos (with differents colors, etc). I would like to use it like the Console.WriteLine("My Message"); on...
3
15535
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 found the follows. Invalid length for a Base-64 char array. Description: An unhandled exception occurred during the execution of the current...
0
1599
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 Console base application project, it automatically created RCW DLL Interop.Fredi.dll. i have deployed this application on Remote machine and...
5
1990
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: *************************************************************************************************************** { ........................... PrintTimeSpanProblem(); device = new Device(0, DeviceType.Hardware, this,...
0
7841
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8204
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7965
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6617
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.