473,387 Members | 1,575 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,387 software developers and data experts.

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(object sender, System.EventArgs e)
{
try
{
Foo();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "button1_Click: Caught exception: ");
throw;
}
}

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

Nov 16 '05 #1
9 4976
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.ThreadException.

so, instead of

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

use

Application.ThreadException += new ThreadExceptionEventHandler(
MyExceptionHandler );
Application.Run(...);

....

public static void MyExceptionHandler( object sender,
ThreadExceptionEventArgs e )

{

Exception ex = e.Exception;

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

Application.Exit();

}

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(object sender, System.EventArgs e)
{
try
{
Foo();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "button1_Click: Caught exception: ");
throw;
}
}

void Foo()
{
throw new Exception("Exception 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.ThreadException += 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.ThreadException += ...
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.com.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.ThreadException += 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.ThreadException += ...
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.ThreadException 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.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use add handler in your Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException 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.ThreadException 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.ThreadException 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.microsoft.com> wrote in message
news:39**********************************@microsof t.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(object sender, System.EventArgs e)
{
try
{
Foo();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "button1_Click: Caught exception: ");
throw;
}
}

void Foo()
{
throw new Exception("Exception 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
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...
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...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
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...
2
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...
9
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...
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...
4
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...
41
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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...

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.