473,396 Members | 1,975 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,396 software developers and data experts.

default exception handler for a class with visibility to its metho

I have Visual Studio C# app where the main form keeps a log of the
application's status. For several of the event handlers and methods in the
main form, I have try/catch blocks. But adding try/catch blocks to every
method is rather painful, especially when most of them do the same thing
anyway; which is to record the exception details to the log and show them in
a TextBox.

I am looking for a way that I can create a default event handler to do this.
The catch is, that the default event handler must have visibility to the
logging function in the scope of the main form.

I have tried adding an event handler to Application.ThreadException like this:

Application.ThreadException += new
ThreadExceptionEventHandler(DefaultExceptionHandle r);

where DefaultExceptionHandler is defined in the main form class and does all
the necessary logging.

The problem is, I needed to place the above code in the constructor of my
form for the DefaultExceptionHandler to be visible. Although this did not
cause any compile or runtime errors, unhandled exceptions did not seem to
call out the DefaultExceptionHandler (as if it wasn't even there). I suspect
this might be because I need to put the above statement in my code BEFORE
Application.Run() is called, and not in the main form constructor. (Please
correct me if I am wrong.) But if I do this, then I no longer have
visibility to my main form, because the Application.Run() is called from the
static class Main.

Also, it is my understanding that the ThreadEventExceptionHandler will
terminate the application. For this app, I can determine whether the
exception that occurred is fatal or not, so I would really like to have
control over whether the app terminates or continues. Can I somehow prevent
the standard ThreadEventExceptionHandler code from executing so that only my
DefaultExceptionHandler executes?

Nov 17 '05 #1
0 922

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

Similar topics

1
by: Nick | last post by:
I am working on a VB.NET application and instead of throwing Try Catch blocks all over the place, I have a custom applcation context which has a exception handler.. Public Class ContextManager...
1
by: | last post by:
I would appreciate some help in understanding the simple C# example relating to handling exceptions. This one relates to catching an error thrown by dividing number by zero. There are a few...
2
by: webdev | last post by:
I'm using the code shown below which displays 3 input fields - when you click on an input field, a DIV is displayed which allows you to select a task. On clicking the task, it is inserted into the...
2
by: Martin Lapierre | last post by:
How can I remove the default exception handler handler? When adding a custom handler, I can't get rid of the VS.NET unhandled exception dialog. Ex: AppDomain currentDomain =...
1
by: Divya | last post by:
Hello This is my 1st project where I have to create a Webcontrol. I have created a simple custom control with a button and 2 labels added to a panel. My problem is that the event handler that I...
2
by: | last post by:
I would appreciate some help in understanding the simple C# example relating to handling exceptions. This one relates to catching an error thrown by dividing number by zero. There are a few...
19
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do...
10
by: Rahul | last post by:
Hi Everyone, I have the following exception class, class E1 { }; class E2 {
3
by: LordHog | last post by:
Hello, How would I go about finding the default handler, let's say a text file (*.txt), then launch the default handler with the file as an argument? I had found how to launch an external...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...
0
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,...

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.