473,408 Members | 2,839 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,408 software developers and data experts.

centralize exception handling without try catch blocks in c#.

please tell the technique of centralize exception handling without try catch
blocks in c#.

Jul 21 '05 #1
7 5940
In your startup method:
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(eh.On ThreadException);
Application.Run(new frmMain());

where eh is an instance of the class that will handle the 'unexpected
exceptions'.

internal void OnThreadException(object sender,
System.Threading.ThreadExceptionEventArgs t)
{
Exception e = t.Exception;
StackTrace st = new StackTrace(e);
.... retrieve the method stack for diagnostic purposes
... display a friendly message, etc.
}

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter
and the Instant VB C# to VB.NET converter

"Noor" wrote:
please tell the technique of centralize exception handling without try catch
blocks in c#.

Jul 21 '05 #2
There's also another event that is fired from the current AppDomain called
UnhandledException - I recommend assigning a delegate to both the Thread and
the AppDomain exception events to limit the chance of the exception not
getting handled.

AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(eh.UnhandledExcepti onHandler);

Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(eh.On ThreadException);
"David Anton" wrote:
In your startup method:
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(eh.On ThreadException);
Application.Run(new frmMain());

where eh is an instance of the class that will handle the 'unexpected
exceptions'.

internal void OnThreadException(object sender,
System.Threading.ThreadExceptionEventArgs t)
{
Exception e = t.Exception;
StackTrace st = new StackTrace(e);
.... retrieve the method stack for diagnostic purposes
... display a friendly message, etc.
}

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter
and the Instant VB C# to VB.NET converter

"Noor" wrote:
please tell the technique of centralize exception handling without try catch
blocks in c#.

Jul 21 '05 #3
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???

After reading your's reply i use both Application.ThreadException and
AppDomain.CurrentDomain.UnhandledException event in order to capture all
types of exception both within IDE and outside the IDE.

Now please guide me whether it is good technique for central exception
handling or i need two startup classes to handle Exception.One for handling
Exception Within the IDE or 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

"Jorge L Matos [MCSD.NET]" <matos_jorge_NOSPAM_AT_hotmail.com> wrote in
message news:62**********************************@microsof t.com...
There's also another event that is fired from the current AppDomain called
UnhandledException - I recommend assigning a delegate to both the Thread
and
the AppDomain exception events to limit the chance of the exception not
getting handled.

AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(eh.UnhandledExcepti onHandler);

Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(eh.On ThreadException);
"David Anton" wrote:
In your startup method:
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(eh.On ThreadException);
Application.Run(new frmMain());

where eh is an instance of the class that will handle the 'unexpected
exceptions'.

internal void OnThreadException(object sender,
System.Threading.ThreadExceptionEventArgs t)
{
Exception e = t.Exception;
StackTrace st = new StackTrace(e);
.... retrieve the method stack for diagnostic purposes
... display a friendly message, etc.
}

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter
and the Instant VB C# to VB.NET converter

"Noor" wrote:
> please tell the technique of centralize exception handling without try
> catch
> blocks in c#.
>
>
>
>

Jul 21 '05 #4
I think having one exception handling class for running inside and outside
the IDE is appropriate and your sample code looks okay to me.

"Noor" wrote:
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???

After reading your's reply i use both Application.ThreadException and
AppDomain.CurrentDomain.UnhandledException event in order to capture all
types of exception both within IDE and outside the IDE.

Now please guide me whether it is good technique for central exception
handling or i need two startup classes to handle Exception.One for handling
Exception Within the IDE or 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

"Jorge L Matos [MCSD.NET]" <matos_jorge_NOSPAM_AT_hotmail.com> wrote in
message news:62**********************************@microsof t.com...
There's also another event that is fired from the current AppDomain called
UnhandledException - I recommend assigning a delegate to both the Thread
and
the AppDomain exception events to limit the chance of the exception not
getting handled.

AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(eh.UnhandledExcepti onHandler);

Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(eh.On ThreadException);
"David Anton" wrote:
In your startup method:
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(eh.On ThreadException);
Application.Run(new frmMain());

where eh is an instance of the class that will handle the 'unexpected
exceptions'.

internal void OnThreadException(object sender,
System.Threading.ThreadExceptionEventArgs t)
{
Exception e = t.Exception;
StackTrace st = new StackTrace(e);
.... retrieve the method stack for diagnostic purposes
... display a friendly message, etc.
}

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter
and the Instant VB C# to VB.NET converter

"Noor" wrote:

> please tell the technique of centralize exception handling without try
> catch
> blocks in c#.
>
>
>
>


Jul 21 '05 #5
There are several potential problems you should be aware of.

First, in the current shipping version of the runtime (v1.1), even though
you can successfully subscribe to the AppDomain.UnhandledException, the
event only gets delivered to handlers that reside in the default appdomaion.
If your code runs in a different appdomain it will never get the event, and
you will never even know that it wont get delivered. This is also true of
several other system notifications, such as the ProcessExit event.

Second, using a UE handler does not really handle the exception, it just
provides a last ditch place for you to get notified that a problem occurred.
It does not allow you to recover from it (except only in the most general
way).

Third, even though UEs are only fatal if it occurs on the main thread of
your application, this will not be true in future releases of the runtime.
This is a policy decision of the application and it will probably change so
that all UEs are fatal unless the runtime is otherwise instructed.

In general I disagree with the notion that it's ok to ingnore error handling
(re: try-catch) on all threads except the main thread because a global piece
of code somewhere else will handle it all for you.
"Noor" <no****@nxvt.com> wrote in message
news:e8*************@TK2MSFTNGP15.phx.gbl...
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???

After reading your's reply i use both Application.ThreadException and
AppDomain.CurrentDomain.UnhandledException event in order to capture all
types of exception both within IDE and outside the IDE.

Now please guide me whether it is good technique for central exception
handling or i need two startup classes to handle Exception.One for
handling Exception Within the IDE or 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

"Jorge L Matos [MCSD.NET]" <matos_jorge_NOSPAM_AT_hotmail.com> wrote in
message news:62**********************************@microsof t.com...
There's also another event that is fired from the current AppDomain
called
UnhandledException - I recommend assigning a delegate to both the Thread
and
the AppDomain exception events to limit the chance of the exception not
getting handled.

AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(eh.UnhandledExcepti onHandler);

Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(eh.On ThreadException);
"David Anton" wrote:
In your startup method:
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(eh.On ThreadException);
Application.Run(new frmMain());

where eh is an instance of the class that will handle the 'unexpected
exceptions'.

internal void OnThreadException(object sender,
System.Threading.ThreadExceptionEventArgs t)
{
Exception e = t.Exception;
StackTrace st = new StackTrace(e);
.... retrieve the method stack for diagnostic purposes
... display a friendly message, etc.
}

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter
and the Instant VB C# to VB.NET converter

"Noor" wrote:

> please tell the technique of centralize exception handling without try
> catch
> blocks in c#.
>
>
>
>


Jul 21 '05 #6
Everyone has a right to their own opinion, but I would add that in general
using a global error handler in combination with selective use of try/catch
works out well enough for most situations.

Insisting on the use of try/catch in every method of every class is just way
too anal retentive.

--
Jorge L. Matos

"David Levine" wrote:
There are several potential problems you should be aware of.

First, in the current shipping version of the runtime (v1.1), even though
you can successfully subscribe to the AppDomain.UnhandledException, the
event only gets delivered to handlers that reside in the default appdomaion.
If your code runs in a different appdomain it will never get the event, and
you will never even know that it wont get delivered. This is also true of
several other system notifications, such as the ProcessExit event.

Second, using a UE handler does not really handle the exception, it just
provides a last ditch place for you to get notified that a problem occurred.
It does not allow you to recover from it (except only in the most general
way).

Third, even though UEs are only fatal if it occurs on the main thread of
your application, this will not be true in future releases of the runtime.
This is a policy decision of the application and it will probably change so
that all UEs are fatal unless the runtime is otherwise instructed.

In general I disagree with the notion that it's ok to ingnore error handling
(re: try-catch) on all threads except the main thread because a global piece
of code somewhere else will handle it all for you.
"Noor" <no****@nxvt.com> wrote in message
news:e8*************@TK2MSFTNGP15.phx.gbl...
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???

After reading your's reply i use both Application.ThreadException and
AppDomain.CurrentDomain.UnhandledException event in order to capture all
types of exception both within IDE and outside the IDE.

Now please guide me whether it is good technique for central exception
handling or i need two startup classes to handle Exception.One for
handling Exception Within the IDE or 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

"Jorge L Matos [MCSD.NET]" <matos_jorge_NOSPAM_AT_hotmail.com> wrote in
message news:62**********************************@microsof t.com...
There's also another event that is fired from the current AppDomain
called
UnhandledException - I recommend assigning a delegate to both the Thread
and
the AppDomain exception events to limit the chance of the exception not
getting handled.

AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(eh.UnhandledExcepti onHandler);

Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(eh.On ThreadException);
"David Anton" wrote:

In your startup method:
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(eh.On ThreadException);
Application.Run(new frmMain());

where eh is an instance of the class that will handle the 'unexpected
exceptions'.

internal void OnThreadException(object sender,
System.Threading.ThreadExceptionEventArgs t)
{
Exception e = t.Exception;
StackTrace st = new StackTrace(e);
.... retrieve the method stack for diagnostic purposes
... display a friendly message, etc.
}

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter
and the Instant VB C# to VB.NET converter

"Noor" wrote:

> please tell the technique of centralize exception handling without try
> catch
> blocks in c#.
>
>
>
>



Jul 21 '05 #7
As you say, it's your opinion and you are entitled to it.

I've never said that every method in every class should have a try-catch -
that would actually be bad, not good....but every thread ought to have one
at the top of the thread, and every place where an exception could be
dropped (e.g. threadpool callback routines) ought to have one. If you choose
not to do that, well, my caring too much about someone else's code really
would be anal retentive.

"Jorge L Matos [MCSD.NET]" <matos_jorge_NOSPAM_AT_hotmail.com> wrote in
message news:1F**********************************@microsof t.com...
Everyone has a right to their own opinion, but I would add that in general
using a global error handler in combination with selective use of
try/catch
works out well enough for most situations.

Insisting on the use of try/catch in every method of every class is just
way
too anal retentive.

--
Jorge L. Matos

"David Levine" wrote:
There are several potential problems you should be aware of.

First, in the current shipping version of the runtime (v1.1), even though
you can successfully subscribe to the AppDomain.UnhandledException, the
event only gets delivered to handlers that reside in the default
appdomaion.
If your code runs in a different appdomain it will never get the event,
and
you will never even know that it wont get delivered. This is also true of
several other system notifications, such as the ProcessExit event.

Second, using a UE handler does not really handle the exception, it just
provides a last ditch place for you to get notified that a problem
occurred.
It does not allow you to recover from it (except only in the most general
way).

Third, even though UEs are only fatal if it occurs on the main thread of
your application, this will not be true in future releases of the
runtime.
This is a policy decision of the application and it will probably change
so
that all UEs are fatal unless the runtime is otherwise instructed.

In general I disagree with the notion that it's ok to ingnore error
handling
(re: try-catch) on all threads except the main thread because a global
piece
of code somewhere else will handle it all for you.
"Noor" <no****@nxvt.com> wrote in message
news:e8*************@TK2MSFTNGP15.phx.gbl...
> 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???
>
> After reading your's reply i use both Application.ThreadException and
> AppDomain.CurrentDomain.UnhandledException event in order to capture
> all
> types of exception both within IDE and outside the IDE.
>
> Now please guide me whether it is good technique for central exception
> handling or i need two startup classes to handle Exception.One for
> handling Exception Within the IDE or 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
>
> "Jorge L Matos [MCSD.NET]" <matos_jorge_NOSPAM_AT_hotmail.com> wrote in
> message news:62**********************************@microsof t.com...
>> There's also another event that is fired from the current AppDomain
>> called
>> UnhandledException - I recommend assigning a delegate to both the
>> Thread
>> and
>> the AppDomain exception events to limit the chance of the exception
>> not
>> getting handled.
>>
>> AppDomain.CurrentDomain.UnhandledException += new
>> UnhandledExceptionEventHandler(eh.UnhandledExcepti onHandler);
>>
>> Application.ThreadException += new
>> System.Threading.ThreadExceptionEventHandler(eh.On ThreadException);
>>
>>
>> "David Anton" wrote:
>>
>>> In your startup method:
>>> Application.ThreadException += new
>>> System.Threading.ThreadExceptionEventHandler(eh.On ThreadException);
>>> Application.Run(new frmMain());
>>>
>>> where eh is an instance of the class that will handle the 'unexpected
>>> exceptions'.
>>>
>>> internal void OnThreadException(object sender,
>>> System.Threading.ThreadExceptionEventArgs t)
>>> {
>>> Exception e = t.Exception;
>>> StackTrace st = new StackTrace(e);
>>> .... retrieve the method stack for diagnostic purposes
>>> ... display a friendly message, etc.
>>> }
>>>
>>> David Anton
>>> www.tangiblesoftwaresolutions.com
>>> Home of the Instant C# VB.NET to C# converter
>>> and the Instant VB C# to VB.NET converter
>>>
>>> "Noor" wrote:
>>>
>>> > please tell the technique of centralize exception handling without
>>> > try
>>> > catch
>>> > blocks in c#.
>>> >
>>> >
>>> >
>>> >
>
>


Jul 21 '05 #8

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

Similar topics

6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
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...
1
by: Noor | last post by:
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...
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...
4
by: sm | last post by:
Hi, I have a couple of questions with regards to handling errors and exceptions. 1. If I use On Error goto Errhandler ... Errhandler:
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Larry Herbinaux | last post by:
I have built an asychronous TCP Server that uses the thread pool. I have two levels of exception handling in case the handling of the inner catch block throws an exception. The outer catch block...
5
by: Bry | last post by:
I've created a class that offers an enhanced way of handling fatal exceptions. The class allows the user to optionaly submit a http based anonymous error report to myself, and also records details...
14
by: jehugaleahsa | last post by:
Hello: As an avid reader of C++ books, I know a lot of programmers out there are confronted with the challenge of exception safety. For those who don't know what it is, it is writing code in...
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
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
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
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...
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.