473,770 Members | 3,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bug in exception handling

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 catch handler.

To repeat, create a new Windows Forms application and put the following code
into Form1.cs.

----

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
Application.Run (new Form1());
}
catch (Exception ex)
{
MessageBox.Show (ex.Message, "Main: Caught exception: ");
}
}

private void button1_Click(o bject sender, System.EventArg s e)
{
try
{
Foo();
}
catch (Exception ex)
{
MessageBox.Show (ex.Message, "button1_Cl ick: Caught exception: ");
throw;
}
}

void Foo()
{
throw new Exception("Exce ption thrown from Foo()");
}

Nov 16 '05 #1
9 5004
Hi David,

I can't say if it is fixed in service pack 1, but the code runs fine in Framework 2.0 both debug and release.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
> Application.Run (new Form1());

it is rather by design of the WinForms that the exceptions thrown outside
the Application.Run are not caught by try-catch but by the thread's global
exception handler set by Application.Thr eadException.

so, instead of

try
{
Application.Run (...)
}
catch() {...}

use

Application.Thr eadException += new ThreadException EventHandler(
MyExceptionHand ler );
Application.Run (...);

....

public static void MyExceptionHand ler( object sender,
ThreadException EventArgs e )

{

Exception ex = e.Exception;

// do something with the exception but you are unlikely to resume the
application

Application.Exi t();

}

regards,

Wiktor Zychla
Nov 16 '05 #3
Hi David,

<snip>
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...
Yeah!
That seems to be real problem in M$.
I work with VS 2002 and .NET Framework 1.0, and only SP 3 for this
framework repairs something for real.

It seems that Windows application doesn't like exceptions in
event handling.
Your sample runs fine in VS 2002 Release mode... but it crashes
on direct execution.

Cheers

Marcin
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 catch handler.

To repeat, create a new Windows Forms application and put the following code
into Form1.cs.

----

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
Application.Run (new Form1());
}
catch (Exception ex)
{
MessageBox.Show (ex.Message, "Main: Caught exception: ");
}
}

private void button1_Click(o bject sender, System.EventArg s e)
{
try
{
Foo();
}
catch (Exception ex)
{
MessageBox.Show (ex.Message, "button1_Cl ick: Caught exception: ");
throw;
}
}

void Foo()
{
throw new Exception("Exce ption thrown from Foo()");
}

Nov 16 '05 #4
Hi Morten,

Try to execute it outside the VS.

Marcin
Hi David,

I can't say if it is fixed in service pack 1, but the code runs fine in
Framework 2.0 both debug and release.

Nov 16 '05 #5
You are right,

It does crash outside VS even with framework 2.0

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #6
Interesting, this works applications run outside Visual Studio, but Visual Studio 2005 does not like it at all and claims the throw before the application exception is unhandled.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #7
> Interesting, this works applications run outside Visual Studio, but Visual
Studio 2005 does not like it at all and claims the throw before the
application exception is unhandled.

wrap the code with Application.Thr eadException += with a try-catch.

this way the code will work standalone (because the ThreadException will
catch the outer exception) and should also work from within the VS.NET
(because the try-catch will catch the outer exception).

try
{
Application.Thr eadException += ...
Application.Run (...);
}
catch( Exception ex ) {...}

it works for me in vs2003 at least.

Wiktor ZYchla
Nov 16 '05 #8
On Thu, 16 Sep 2004 12:50:00 +0200, Wiktor Zychla <us**@nospam.co m.eu> wrote:
Interesting, this works applications run outside Visual Studio, but Visual

Studio 2005 does not like it at all and claims the throw before the
application exception is unhandled.

wrap the code with Application.Thr eadException += with a try-catch.

this way the code will work standalone (because the ThreadException will
catch the outer exception) and should also work from within the VS.NET
(because the try-catch will catch the outer exception).

try
{
Application.Thr eadException += ...
Application.Run (...);
}
catch( Exception ex ) {...}

it works for me in vs2003 at least.

Wiktor ZYchla


This also works in 2005. Very handy piece of code. Thanks :)

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #9
David,
Your try/catch in your Main is only catching exceptions that the constructor
of Form1 raises!

The exception that your button1_Click raises is not handled, VS.NET handles
the exception for you, so as to allow you to debug the problem with your
program.

You need to handle the Application.Thr eadException event in your Main to
catch unhandled that button1_Click raises.

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.Http Application.Err or event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomai n.UnhandledExce ption event
Use add handler in your Main.

For Windows Forms look at:
System.Windows. Forms.Applicati on.ThreadExcept ion event
Use add handler in your Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the
Application.Thr eadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.Thr eadException handler will catch all
uncaught exceptions from any form/control event handlers.

Note David has some excellent comments on argument validation to your class
libraries. Especially if those class libraries are going to be used outside
of your current solution.

Hope this helps
Jay

"David B" <David B@discussions.m icrosoft.com> wrote in message
news:39******** *************** ***********@mic rosoft.com...
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 catch handler.

To repeat, create a new Windows Forms application and put the following
code
into Form1.cs.

----

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
Application.Run (new Form1());
}
catch (Exception ex)
{
MessageBox.Show (ex.Message, "Main: Caught exception: ");
}
}

private void button1_Click(o bject sender, System.EventArg s e)
{
try
{
Foo();
}
catch (Exception ex)
{
MessageBox.Show (ex.Message, "button1_Cl ick: Caught exception: ");
throw;
}
}

void Foo()
{
throw new Exception("Exce ption thrown from Foo()");
}

Nov 16 '05 #10

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

Similar topics

11
2886
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 return code (not generating error/exception) so it is more compatible with other programming language.
6
2341
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 assemblies are loaded before shutting down. In one case, some of my DB-accessing code didn't handle a NULL value properly. But try...catch wouldn't catch the exception and keep going. I'd just get the error message and then it would shut the...
7
6007
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2751
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 exception handling mechanism which will allow me to pass character information about an error and its location from lower-level classes. Can you please critique the following exception handling mechanism in terms of my requirements ?
2
2595
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 Layer. My questions is where should I put exception handling: 1) Should it be put in all significant methods in all layers? 2) Should I create an exception base class that will handle the errors and pass useful error messages to the user?
9
2538
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 the way of CPU usage), compared to the "normal" practise returning values. How true is this? Will using using exception handling, in general, be much less efficient than returning values, or less efficient at all? Just curious...
44
4228
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
5136
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 .NET 2003\Vc7\include\xstring(1453) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
41
3076
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 handle all of them. I don't care about which one happened, except to write out exception.Message to a log file. It seems verbose to write out three handlers that all do the same thing. So, I could just catch Exception. But, is that...
1
3109
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
10057
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
10002
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
9869
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...
1
7415
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
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
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
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.