473,804 Members | 3,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# 2.0 Exception Handling - Can't Understand MSDN Example

Hi There

I'm probably missing something fundamental here but I don't understand
the code in the OnThreadExcepti on routine, which is found in MSDN under
the topic: Application.Thr eadException Event

I think the whole OnThreadExcepti on routine could be replaced with two
lines of code:

if (ShowThreadExce ptionDialog(t.E xception) == DialogResult.Ab ort)
Environment.Exi t(0);

Why? Because:
(1) FXCop complains about the MSDN example with CA1031 :
Microsoft.Desig n : Modify ... to catch a more specific exception than
'System.Object' or rethrow the exception.
(2) I don't understand why they even bother catching an exception as
they just give a totally useless message and terminate the application
anyways:

try
{
MessageBox.Show ("Fatal Error", "Fatal Error" ...
}
finally
{
Application.Exi t();
}

(3) Environment.Exi t is more relevant for the project I am working on
as Application.Exi t(0) only seems to end the blocking
Application.Run (new myMainForm()) line. If there is code after the
Application.Run line it will still be executed. However I want to give
the application a "bullet in the head".

Thanks In Advance
Bill

Nov 17 '05 #1
3 5044
Actually, I just realized a design flaw in the sample code:

try
{
result = this.ShowThread ExceptionDialog (t.Exception);
}
catch
{
try
{
MessageBox.Show ("Fatal Error", "Fatal Error",
MessageBoxButto ns.AbortRetryIg nore, MessageBoxIcon. Stop);
}
finally
{
Application.Exi t();
}
}

In the second try clause, why give the user the option to Abort / Retry
/ Ignore if the finally clause is just going to Application.Exi t()
anyway ?

Nov 17 '05 #2
Bill,

Regarding giving your app a "bullet in the head", you would have to call
the static Exit method on the Environment class. However, I wouldn't
recommend this.

When you call Application.Exi t, you are sending a WM_QUIT message to the
message loop, which then terminates the loop, and the code after your loop
executes.

What you should do is set a flag that can be read after the loop
executes, and then call Application.Exi t (setting the flag before you make
the call). When the call to Application.Run completes (this is your message
loop), check the flag. If it is true, then just return.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<or*******@yaho o.com.au> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hi There

I'm probably missing something fundamental here but I don't understand
the code in the OnThreadExcepti on routine, which is found in MSDN under
the topic: Application.Thr eadException Event

I think the whole OnThreadExcepti on routine could be replaced with two
lines of code:

if (ShowThreadExce ptionDialog(t.E xception) == DialogResult.Ab ort)
Environment.Exi t(0);

Why? Because:
(1) FXCop complains about the MSDN example with CA1031 :
Microsoft.Desig n : Modify ... to catch a more specific exception than
'System.Object' or rethrow the exception.
(2) I don't understand why they even bother catching an exception as
they just give a totally useless message and terminate the application
anyways:

try
{
MessageBox.Show ("Fatal Error", "Fatal Error" ...
}
finally
{
Application.Exi t();
}

(3) Environment.Exi t is more relevant for the project I am working on
as Application.Exi t(0) only seems to end the blocking
Application.Run (new myMainForm()) line. If there is code after the
Application.Run line it will still be executed. However I want to give
the application a "bullet in the head".

Thanks In Advance
Bill

Nov 17 '05 #3
Thanks Nicholas.

Nov 17 '05 #4

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

Similar topics

6
746
by: Jesper Ordrup Christensen | last post by:
Say I've created a piece of code that involves both sql, io and some number conversions. Being a responsible developer I have tried to catch all of the exceptions - but how can I be sure? Is there a way to list exactly which exceptions that are not handlet? (design time) I could of cause ... use catch (Exception e) {...} as the final catch ... but I would prefer to now the "landscape" before doing that ..
9
5006
by: David B | last post by:
Why is it so difficult to report bugs to Microsoft? I have a documented bug and an small test example. I don't really see why I should have to pay to tell them about it... Anyway, the following code exhibits the bug. If it is built and run from Visual Studio.NET 2003 in debug mode it works correctly as expected. However, if built in Release mode it fails with an "uncaught exception" error, although the exception does have a valid...
44
4235
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 user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
4
2411
by: Frank | last post by:
Hi, I made a handler for unhandled errors. But before that is executed, VB.NET gives me the standard error window. In VB6 there was a setting (errortrapping) about handling errors in the design environment and classes, which should prevent the before mentioned behaviour. Does VB.NET have a setting like it? Or is there something else? Thanks in advance Frank
4
2045
by: Rob Richardson | last post by:
Greetings! I am working on an application that targets a Pocket PC running Windows CE and SQL Server CE. Almost all functions in the application use a Try block with a Catch block that looks like this: Try TryToDoIt() Catch e as Exception LogTheError(e)
2
1326
by: Nak | last post by:
Hi there, I'm just curious as to something. I have just added an exception handler at the entry point to my application, within the IDE any unhandled exceptions fallback to this and enable me to disable a dialog of my liking. Now if I run the application outside of the IDE I recieve the standard "Unhandled exception" dialog provided by .NET giving me the ability to continue or quit. I wasn't actually aware that exception handling...
11
5605
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
5
3833
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 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...
6
4401
by: Steve | last post by:
Hi All I have a windows forms Application (SAM) in vb.net 2008 using .net framework V2 One and only one customer out of 30 customers is getting errors daily where they have to close and restart my application, sometimes several times a day The customer has XP Home SP2
0
9588
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10327
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5527
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
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 we have to send another system
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2999
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.