Exceptional Exceptions
-
Hi, My understanding of the way exception work in .NET is that once an exception is thrown, the run-time unwinds the call stack until either (a) the call stack is empty (game over) or... (b) a catch block is found that "handles" the thrown exception I tested this my understanding of this with the program below. And just as I thought it would, after level5() through the exception the Runtime unwound the call stack back to level1(). My understanding is that the objectless "catch" will catch -ALL- exceptions. Period. Even unmanaged exception one of my books says. So... if I define my static Main() as: static void Main() { try { Application.Run(new Form1()); } catch { MessageBox.Show("Not so fast!"); } } ... is it possible for an (some) execptions to "leak" out to the Runtime? Thanks, Cunfewsdish =============================================================== using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace Exceptional.cs { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // level1(); } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "Form1"; } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } private void level1() { try { level2(); } catch { MessageBox.Show("Kawt!", "Level 1"); }
-
Hi, My understanding of the way exception work in .NET is that once an exception is thrown, the run-time unwinds the call stack until either (a) the call stack is empty (game over) or... (b) a catch block is found that "handles" the thrown exception I tested this my understanding of this with the program below. And just as I thought it would, after level5() through the exception the Runtime unwound the call stack back to level1(). My understanding is that the objectless "catch" will catch -ALL- exceptions. Period. Even unmanaged exception one of my books says. So... if I define my static Main() as: static void Main() { try { Application.Run(new Form1()); } catch { MessageBox.Show("Not so fast!"); } } ... is it possible for an (some) execptions to "leak" out to the Runtime? Thanks, Cunfewsdish =============================================================== using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace Exceptional.cs { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // level1(); } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "Form1"; } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } private void level1() { try { level2(); } catch { MessageBox.Show("Kawt!", "Level 1"); }
If there is an Exception in the Runtime itself, it may crash your app, but an exception from inside your app can't leak out to the Runtime if you have an expcetion handler in place to grab it. There is an exception though. Not all exceptions will propogate up the call stack. The .NET Framework wraps and applications message pump in an exception handler. If an exception is generated that the application code doesn't handle, the exception handler around the message pump will handle it. For an example, you have two forms, one is your main form and the other is a dialog form. This means an exception generated in the dialog form will NOT propogate up to the main form that Show()'d it. Understand? If not, check this[^] article on MSDN for a quick example demonstrating this. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
If there is an Exception in the Runtime itself, it may crash your app, but an exception from inside your app can't leak out to the Runtime if you have an expcetion handler in place to grab it. There is an exception though. Not all exceptions will propogate up the call stack. The .NET Framework wraps and applications message pump in an exception handler. If an exception is generated that the application code doesn't handle, the exception handler around the message pump will handle it. For an example, you have two forms, one is your main form and the other is a dialog form. This means an exception generated in the dialog form will NOT propogate up to the main form that Show()'d it. Understand? If not, check this[^] article on MSDN for a quick example demonstrating this. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Thanks Dave. I was not aware that .NET wrapped each msg pump with an exception handler although that makes good sense. Is it possible to instruct (using your example of 2 forms, main and dialog) the dialog's wrapped exceptional handler to "hand off" caught exceptions back to the main form's code? TIA, Matt
-
Thanks Dave. I was not aware that .NET wrapped each msg pump with an exception handler although that makes good sense. Is it possible to instruct (using your example of 2 forms, main and dialog) the dialog's wrapped exceptional handler to "hand off" caught exceptions back to the main form's code? TIA, Matt
No, it's not. The message pump handler can't be bypassed, unfortunately, and it's behavior is to put up the usual message box you see for an "unhandled" exception. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome