473,503 Members | 241 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5967
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
2322
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
4159
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
1963
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
1367
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
1819
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
511
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
3177
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
3814
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
2088
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
7203
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
7089
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
7339
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
5581
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,...
1
5017
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...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.