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

centralize way of exception handling

Hi all,

I am trying to catch all types of exceptions from a app regardless of
whether it is in debugger mode( VS development environment) or run the.exe
file outside the IDE.
My App contains the thousand of classes and I do not want to use the Try
Catch block in each class. Because it is logically similar to GOTO
statement. I want to prevent exception from being swallowed.

I use a startup project, so that i can show the MDI main application window
within a Try Catch block. This all works fine when i run the App from within
the IDE, but when i run outside of the IDE i.e. run the .exe file. It
doesn't seem to catch the exception???

I also use both Application.ThreadException and
AppDomain.CurrentDomain.UnhandledException event in order to capture
unhandled Exceptions outside the IDE.

Now please guide me whether it is good technique for centralize way of
exception handling or I need two startup classes to handle Exception. One
for handling Exception within the IDE and one for outside the IDE.

Here is my sample code for handling all types of exception within one
startup class.

try {

Application.ThreadException += new
ThreadExceptionEventHandler(App_ThreadException);

AppDomain.CurrentDomain.UnhandledException +=new
UnhandledExceptionEventHandler(App_UnhandledExcept ion);

Application.Run();

} catch(Exception ex) {

//write nice error reporting routine!

Environment.Exit(0);

}

private static void App_ThreadException(object sender,
ThreadExceptionEventArgs e) {

//show message Box

}

private static void App_UnhandledException(object
sender,UnhandledExceptionEventArgs e) {

//show message Box

}

Hope this makes sense.

Waiting for your reply.

thanks inadvance.

Regards

Noor

Nov 17 '05 #1
1 1950
Yes... using Application.ThreadException and
AppDomain.Unhand*ledException is the MS recommended way of doing
global exception handling. You are doing the right thing.

I went one step further and created a separate Singleton class called
"ErrorLog" that applications can call to set up error handling.
ErrorLog offers different static methods to set up different error
handling for Windows applications, Console applications, and Windows
services applications. It also sets up error handlers only when the
application is not running under the debugger, because when you're in
the debugger you _want_ exceptions to percolate to the top so that the
debugger can break on the excpetion point.

The methods in ErrorLog look like this:

public void MessageBoxLogAndAbortUnhandledExceptions(string
logFilePath)
{
if (!System.Diagnostics.Debugger.IsAttached)
{
this.errorLogFilePath = logFilePath;
Application.ThreadException += new
ThreadExceptionEventHandler(this.OnThreadException MessageBoxLogAndAbort);
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(OnUnhandledExceptio nMessageBoxLogAndAbort);
}
}
private void OnThreadExceptionMessageBoxLogAndAbort(object sender,
ThreadExceptionEventArgs te)
{
ExceptionMessageBoxLogAndAbort(te.Exception.ToStri ng());
}

private void OnUnhandledExceptionMessageBoxLogAndAbort(object sender,
UnhandledExceptionEventArgs ue)
{
Exception ex = ue.ExceptionObject as Exception;
if (ex == null)
{
ExceptionMessageBoxLogAndAbort(String.Format("Exce ption: {0}, Text:
'{1}'", ue.ExceptionObject.GetType(), ue.ExceptionObject.ToString()));
}
else
{
ExceptionMessageBoxLogAndAbort(ex.ToString());
}
}

private void ExceptionMessageBoxLogAndAbort(string msg)
{
DialogResult result = DialogResult.Cancel;
try
{
string message = "An error occurred. Please contact IS with the
following information:\n\n" + msg;
result = MessageBox.Show(message, "Application Error",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
catch
{
// Really nothing to do here... couldn't show a message
// box, so no recovery is possible. Just catch the exception
// and move on....
}
finally
{
try
{
Log(msg);
}
catch
{
try
{
MessageBox.Show("Could not write to log file " + this.LogFilePath,
"Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
catch
{
// Really nothing to do here. Just absorb the exception.
}
}
finally
{
Application.Exit();
}
}
}

Nov 17 '05 #2

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

Similar topics

11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
2
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
4
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio...
4
by: Atul Bahl | last post by:
We want to use one static class that can be called from anywhere in application and provides all the notification mechanism behind the scenes and can be used to handle exceptions all over the...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.