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

exception bubbling

I have a simple app that opens in Program.cs with

try
{
Application.Run(new Shell());
}
catch (Exception ex)
{
LogErr(ex)
}

In the development environment, any exception thrown anywhere in any
form fires the catch in Program.cs. Create a button in Shell that
opens a new form Test. Test has one button that throws a new
exception. When running in the design environment, the exception
rises to the catch in Program.cs. If I run the executable, the throw
results in an unhandled exception error box.

Why would this behavior be different from one environment to the
other? How do I get the executable to react to errors in the same
manner as the development environment?

Thank you!
J

Feb 7 '07 #1
4 2149
maybe LogErr is actually throwing an error of its own....
Feb 8 '07 #2
On Feb 7, 12:50 pm, jay.meerd...@gmail.com wrote:
I have a simple app that opens in Program.cs with

try
{
Application.Run(new Shell());
}
catch (Exception ex)
{
LogErr(ex)
}

In the development environment, any exception thrown anywhere in any
form fires the catch in Program.cs. Create a button in Shell that
opens a new form Test. Test has one button that throws a new
exception. When running in the design environment, the exception
rises to the catch in Program.cs. If I run the executable, the throw
results in an unhandled exception error box.

Why would this behavior be different from one environment to the
other? How do I get the executable to react to errors in the same
manner as the development environment?

Thank you!
J
Look at the stack dump for the exception when you're not running under
the debugger: your button's handler isn't being called by your
application, is it? It's being called by Windows.

This has to do with how the Windows message pump works. The operating
system subverts the apparent control flow in your program so that it
has time in between to do things like redraw windows, minimize,
resize, and move things around.

The apparent control flow is maintained in the debugger to help you
find errors, but it's not the real way that your program works.

Instead of the catch-all that you have around your Application.Run
call, you should use the two global exception events:
AppDomain.UnhandledException and Application.ThreadException. Search
for these in this newsgroup, or on MSDN, and you'll find documentation
about how to add global exception handlers to your program.

Feb 8 '07 #3
I just happen to have an app that does this. Here's some sample code:

AppDomain.CurrentDomain.UnhandledException += // CLR
new UnhandledExceptionEventHandler(OnUnhandledExceptio n);

Application.ThreadException += // Windows Forms
new System.Threading.ThreadExceptionEventHandler(
OnGuiUnhandedException);
// CLR unhandled exception
private static void OnUnhandledException(Object sender,
UnhandledExceptionEventArgs e)
{
HandleUnhandledException(e.ExceptionObject);
}

// Windows Forms unhandled exception
private static void OnGuiUnhandedException(Object sender,
System.Threading.ThreadExceptionEventArgs e)
{
HandleUnhandledException(e.Exception);
}

static void HandleUnhandledException(Object o)
{
Exception e = o as Exception;

if (e != null)
{ // Report System.Exception info
//Debug.WriteLine("Exception = " + e.GetType());
//Debug.WriteLine("Message = " + e.Message);
//Debug.WriteLine("FullText = " + e.ToString());
Utilities.HandleError(e);
}
else
{ // Report exception Object info
//Debug.WriteLine("Exception = " + o.GetType());
//Debug.WriteLine("FullText = " + o.ToString());
Utilities.LogError("Exception: " + o.GetType().ToString() +
"\r\nFullText: " + o.ToString());
}

MessageBox.Show("An unhandled exception occurred " +
"and the application is shutting down.");
Environment.Exit(1); // Shutting down
}

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

The shortest distance between 2 points is a curve.
"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
On Feb 7, 12:50 pm, jay.meerd...@gmail.com wrote:
>I have a simple app that opens in Program.cs with

try
{
Application.Run(new Shell());
}
catch (Exception ex)
{
LogErr(ex)
}

In the development environment, any exception thrown anywhere in any
form fires the catch in Program.cs. Create a button in Shell that
opens a new form Test. Test has one button that throws a new
exception. When running in the design environment, the exception
rises to the catch in Program.cs. If I run the executable, the throw
results in an unhandled exception error box.

Why would this behavior be different from one environment to the
other? How do I get the executable to react to errors in the same
manner as the development environment?

Thank you!
J

Look at the stack dump for the exception when you're not running under
the debugger: your button's handler isn't being called by your
application, is it? It's being called by Windows.

This has to do with how the Windows message pump works. The operating
system subverts the apparent control flow in your program so that it
has time in between to do things like redraw windows, minimize,
resize, and move things around.

The apparent control flow is maintained in the debugger to help you
find errors, but it's not the real way that your program works.

Instead of the catch-all that you have around your Application.Run
call, you should use the two global exception events:
AppDomain.UnhandledException and Application.ThreadException. Search
for these in this newsgroup, or on MSDN, and you'll find documentation
about how to add global exception handlers to your program.

Feb 8 '07 #4
On Feb 8, 8:06 am, "Kevin Spencer" <unclechut...@nothinks.comwrote:
I just happen to have an app that does this. Here's some sample code:

AppDomain.CurrentDomain.UnhandledException += // CLR
new UnhandledExceptionEventHandler(OnUnhandledExceptio n);

Application.ThreadException += // Windows Forms
new System.Threading.ThreadExceptionEventHandler(
OnGuiUnhandedException);

// CLR unhandled exception
private static void OnUnhandledException(Object sender,
UnhandledExceptionEventArgs e)
{
HandleUnhandledException(e.ExceptionObject);

}

// Windows Forms unhandled exception
private static void OnGuiUnhandedException(Object sender,
System.Threading.ThreadExceptionEventArgs e)
{
HandleUnhandledException(e.Exception);

}

static void HandleUnhandledException(Object o)
{
Exception e = o as Exception;

if (e != null)
{ // Report System.Exception info
//Debug.WriteLine("Exception = " + e.GetType());
//Debug.WriteLine("Message = " + e.Message);
//Debug.WriteLine("FullText = " + e.ToString());
Utilities.HandleError(e);
}
else
{ // Report exception Object info
//Debug.WriteLine("Exception = " + o.GetType());
//Debug.WriteLine("FullText = " + o.ToString());
Utilities.LogError("Exception: " + o.GetType().ToString() +
"\r\nFullText: " + o.ToString());
}

MessageBox.Show("An unhandled exception occurred " +
"and the application is shutting down.");
Environment.Exit(1); // Shutting down

}

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composerhttp://unclechutney.blogspot.com

The shortest distance between 2 points is a curve.

"Bruce Wood" <brucew...@canada.comwrote in message

news:11**********************@a75g2000cwd.googlegr oups.com...
On Feb 7, 12:50 pm, jay.meerd...@gmail.com wrote:
I have a simple app that opens in Program.cs with
try
{
Application.Run(new Shell());
}
catch (Exception ex)
{
LogErr(ex)
}
In the development environment, any exception thrown anywhere in any
form fires the catch in Program.cs. Create a button in Shell that
opens a new form Test. Test has one button that throws a new
exception. When running in the design environment, the exception
rises to the catch in Program.cs. If I run the executable, the throw
results in an unhandled exception error box.
Why would this behavior be different from one environment to the
other? How do I get the executable to react to errors in the same
manner as the development environment?
Thank you!
J
Look at the stack dump for the exception when you're not running under
the debugger: your button's handler isn't being called by your
application, is it? It's being called by Windows.
This has to do with how the Windows message pump works. The operating
system subverts the apparent control flow in your program so that it
has time in between to do things like redraw windows, minimize,
resize, and move things around.
The apparent control flow is maintained in the debugger to help you
find errors, but it's not the real way that your program works.
Instead of the catch-all that you have around your Application.Run
call, you should use the two global exception events:
AppDomain.UnhandledException and Application.ThreadException. Search
for these in this newsgroup, or on MSDN, and you'll find documentation
about how to add global exception handlers to your program.- Hide quoted text -

- Show quoted text -
Thanks all!

Feb 8 '07 #5

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

Similar topics

14
by: Michael Winter | last post by:
In an attempt to answer another question in this group, I've had to resort to calling the DOM method, Node.removeChild(), using a reference to it (long story...). That is, passing Node.removeChild....
3
by: David | last post by:
Hi, Ive been trying to work this out for the past 2 days now and im not getting anywhere fast. The problem i have is that i am using Asynchronous sockets to create a Socket Client library....
28
by: jakk | last post by:
Hello All, I have a question about how to handle exceptions. Iam working on an ASP.NET application which has a presentation layer, Business Layer and DataAccess Layer. Iam confused about where...
7
by: comzy | last post by:
I have created an event bubbling for my pager control which is used for implementing paging in data grid. Althoug it worked very fine in .NET 1.1 it is throwing the following error after i migrated...
5
by: hillcountry74 | last post by:
Hi, I'm a newbie to OOP and VB.Net. In the business layer, I have a base class, which is an abstract class in VB.Net. The derived class overrides a method and calls other methods in the base...
3
by: Nick | last post by:
Hi there, This probably wont make much sense but I'm getting an unhandled exception being thrown in the following line... Try While True Redraw control as necessary End While Catch ex as...
4
by: pamelafluente | last post by:
Hi guys, When bubbling some exception up to some interface handlers - what is most recommandable: Try 'some code Catch ex As Exception
0
by: Ben Crinion | last post by:
Hi Im having a bit of a problem with an exception that doesnt seem to be bubbling up from a user control to its containing user control. The exception can occur on an event fired by a user...
2
by: alhalayqa | last post by:
hi, in MSIE, you can attach onclick event to both anchor and document objects. here is some code : document.onclick = function() {alert('document clicked ...'); } <a href= " " ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.