473,508 Members | 4,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Alternative to Exception Handling

Bry
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 in the
application log. The static method is overloaded, and supports passing
exceptions and/or strings just like throwing an exception.The class
will also fall back to the standard exception handling if something
goes wrong in my class.

As an example of how I use my exception class, I might use something
like this

try
{
// This code should throw an exception

int a = 1;
int b = 0;
int c = a / b;
}
catch (DivideByZeroException ex)
{
EnhancedException.Generate("Application Name", ex);
}

The question is, after my code has dealt with the exception, is it safe
for me to use Enviroment.Exit(1); rather than throwing another
exception to exit my app?

Throwing another exception would confuse the user (seeing my exception
dialog, followed by the system exception dialog)

Note that I only intend to use this in WIndows forms applications.

Nov 5 '06 #1
5 3815
See this post:
http://groups.google.com/group/micro...6814ee621cb99c
Or
http://groups.google.com/groups/sear...tup+form+is%22
This will catch any un caught exceptions, and allow you to provide a
friendly message to your user.


"Bry" <br*********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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 in the
application log. The static method is overloaded, and supports passing
exceptions and/or strings just like throwing an exception.The class
will also fall back to the standard exception handling if something
goes wrong in my class.

As an example of how I use my exception class, I might use something
like this

try
{
// This code should throw an exception

int a = 1;
int b = 0;
int c = a / b;
}
catch (DivideByZeroException ex)
{
EnhancedException.Generate("Application Name", ex);
}

The question is, after my code has dealt with the exception, is it safe
for me to use Enviroment.Exit(1); rather than throwing another
exception to exit my app?

Throwing another exception would confuse the user (seeing my exception
dialog, followed by the system exception dialog)

Note that I only intend to use this in WIndows forms applications.

Nov 5 '06 #2
Hi,
The question is, after my code has dealt with the exception, is it safe
for me to use Enviroment.Exit(1); rather than throwing another
exception to exit my app?
It depends on what you mean by "safe" and what you expect the Exit method to
actually do.

Environment.Exit will immediately stop the process, while Application.Exit
performs a more graceful shutdown of all message loops in the application,
giving each Form the chance to cancel the entire shutdown process if
necessary.
Throwing another exception would confuse the user (seeing my exception
dialog, followed by the system exception dialog)
Do you really want to exit the application when a DivideByZeroException is
caught?

In other cases, where the Exception is critical and cannot be handled by the
application or the end-user, you may want to display a custom dialog and then
exit immediately after, as you've suggested. Throwing another exception just
to exit the application isn't a good idea, IMO.

--
Dave Sexton

"Bry" <br*********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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 in the
application log. The static method is overloaded, and supports passing
exceptions and/or strings just like throwing an exception.The class
will also fall back to the standard exception handling if something
goes wrong in my class.

As an example of how I use my exception class, I might use something
like this

try
{
// This code should throw an exception

int a = 1;
int b = 0;
int c = a / b;
}
catch (DivideByZeroException ex)
{
EnhancedException.Generate("Application Name", ex);
}

The question is, after my code has dealt with the exception, is it safe
for me to use Enviroment.Exit(1); rather than throwing another
exception to exit my app?

Throwing another exception would confuse the user (seeing my exception
dialog, followed by the system exception dialog)

Note that I only intend to use this in WIndows forms applications.

Nov 5 '06 #3
Bry
Thanks for the replies.

Sloan: I wondered about putting a general catch all handler in place,
but I tend to use lots of individual try/catch blocks around anything
that might throw an exception. I might try putting a try/catch around
Application.Run and individual try/catch around other blocks of code
where an exception can be handled better.

Dave: You are quite right, DivideByZero could be handled better, it was
just the first way of generating an exception that came in to my head -
Bad example on my part.

I guess Environment.Exit is better when dealing with fatal exceptions.

Nov 5 '06 #4
Hi,
Sloan: I wondered about putting a general catch all handler in place,
but I tend to use lots of individual try/catch blocks around anything
that might throw an exception. I might try putting a try/catch around
Application.Run and individual try/catch around other blocks of code
where an exception can be handled better.
You really shouldn't catch exceptions that "might" be thrown unless you can
handle them gracefully in code.

Check out the following article for the recommended "local" exception handling
procedures:

"Exception Handling"
http://msdn2.microsoft.com/en-us/lib...05(VS.80).aspx

Adding an event handler to AppDomain.CurrentDomain.UnhandledException is a
good place to catch all instead of wrapping Application.Run in a try...catch.

You might rather use a pre-built library to handle exceptions on a "global"
level:

"Exception Handling Application Block"
http://msdn.microsoft.com/library/de.../html/ehab.asp
Dave: You are quite right, DivideByZero could be handled better, it was
just the first way of generating an exception that came in to my head -
Bad example on my part.

I guess Environment.Exit is better when dealing with fatal exceptions.
In most cases you won't be able to do much with true "fatal" exceptions, so
don't worry about those and just worry about the exceptions that you can
handle, even at the AppDomain level. For instance, what do you expect to do
when an OutOfMemoryException, ExecutionEngineException or
StackOverflowException is thrown?

If one of the aforementioned exceptions is thrown then your application might
just blow up but there's nothing you can really do in terms of "handling"
those exceptions so neither Application.Exit or Environment.Exit should need
to be called. In most other cases you will probably be able to at least log
the exception and try to continue running the application, but it's doubtful
that you'll be able to determine whether there will be any loss of state from
within a "global" exception handler. Therefore, calling Application.Exit will
at least allow your Forms one last change to gracefully shutdown if you must
shutdown the application (but I'd question your reasoning for doing so in the
first place).

I don't think you should worry about trying to end the application in code.
Instead, worry about how to safely keep it running.

--
Dave Sexton

"Bry" <br*********@gmail.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
Thanks for the replies.

Sloan: I wondered about putting a general catch all handler in place,
but I tend to use lots of individual try/catch blocks around anything
that might throw an exception. I might try putting a try/catch around
Application.Run and individual try/catch around other blocks of code
where an exception can be handled better.

Dave: You are quite right, DivideByZero could be handled better, it was
just the first way of generating an exception that came in to my head -
Bad example on my part.

I guess Environment.Exit is better when dealing with fatal exceptions.

Nov 5 '06 #5


Do a google search for

Brad Abrams try finally

He recommends using more

try
{}
finally
{}

instead of
try
{}
catch{}
finally{}
It not fruitful to catch an exception if you're not really going to do
anything with it, except rethrow it.


"Bry" <br*********@gmail.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
Thanks for the replies.

Sloan: I wondered about putting a general catch all handler in place,
but I tend to use lots of individual try/catch blocks around anything
that might throw an exception. I might try putting a try/catch around
Application.Run and individual try/catch around other blocks of code
where an exception can be handled better.

Dave: You are quite right, DivideByZero could be handled better, it was
just the first way of generating an exception that came in to my head -
Bad example on my part.

I guess Environment.Exit is better when dealing with fatal exceptions.

Nov 6 '06 #6

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

Similar topics

7
5968
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2736
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...
9
2522
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...
4
985
by: Anirudha Vengurlekar | last post by:
Hello Community, We can have nested TRY , CATCH , FINALLY blocks in the application. So in what situation (in a class or in a function) I would use this nested Try catch blocks? If someone...
4
5109
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...
2
2918
by: John Smith | last post by:
When implementing exception handling which one of these two alternative ways of exception-handling is the better one? ****************************************************** Alt1)...
41
3022
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
1
3089
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{
35
3469
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
0
7128
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
7332
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,...
1
7058
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
5635
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,...
0
4715
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
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1565
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 ...
0
426
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.