Exception not caught by a Try-Catch block

Exception not caught by a Try-Catch block

Post by amF2aWd1dG » Sat, 06 Jan 2007 02:23:03


Hi,

I'm having trouble with exceptions, apparently in some cases even if I have
a Try Catch block they skip it and bubble up to the CLR. Here's the sample
steps to reproduce. I observed this behaviour oin .NET2.0 VS2005 but happens
in .NET1.1 as well:

1. Create a new VB WinForms application
2. Open the file Form1.vb and replace all its code by this

Public Class Form1
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Dim f As New Form2
Try
f.ShowDialog()
Catch ex As Exception
MessageBox.Show("Exception Caught in Form1")
End Try
End Sub
End Class

3. Add a new form, "Form2.vb" to the project.
4. Open the file Form2.vb and replace all its code by this

Public Class Form2
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Throw New Exception("My Exception")
End Sub
End Class

5. Start With Debug (F5). As expetcted, the exception thrown in Form2 is
caught by Form1 and the messagebox "Exception Caught in Form1" is shown.
Close the messagebox.

6. Start Without Debugging (Ctrl + F5). THE EXCEPTION SKIPS THE TRY CATCH
BLOCK so becomes an Unhandled Exception. It bubbles up to the CLR which shows
the usual ugly crash messagebox.

Anybody can explain to me the reason of this behaviour? Why is the exception
not caught by my TryCatch block??
 
 
 

Exception not caught by a Try-Catch block

Post by Q2lhcmFuIE » Sat, 06 Jan 2007 19:34:00

Sadly, this silly behaviour is by design:
http://www.yqcomputer.com/

I personally think the design should be changed so the .NET exception
handler on the child forms message pump can be confgured to look up the call
stack for another form or a call to show/showdialog and rethrow if found.


--
Ciaran O'Donnell
http://www.yqcomputer.com/

 
 
 

Exception not caught by a Try-Catch block

Post by amF2aWd1dG » Sat, 06 Jan 2007 20:51:00

Thanks Ciaran,

I was much afraid that I would get such an answer, "by design" fits many
bugs :P. And it hasn't been fixed in .NET2.0 so I guess it's going to be like
this for a while, I'll have to find an alternative way to do this (but surely
it won't be near as elegant as with exceptions)

thanks again for your response
javi
 
 
 

Exception not caught by a Try-Catch block

Post by Q2lhcmFuIE » Sat, 06 Jan 2007 23:58:01

understand microsofts point about the design of this one. For a forms
event, the best/only place to catch all unhandled exceptions is the message
loop as everything stems from there. My suggestion for this is an enahncement
not a fix and would cost performance for people to use to (hence making it
configurable).

The best way I see to do this is to catch the exception and return the
dialogresult as Abort or Abort | No | Retry

You could expose the exception caught in a property if you needed it, then
potentially rethrow it.
I wrote a little form which you could use / change to base class which makes
it work but it stops you using one of the dialog results:


public partial class Form2 : Form
{

readonly DialogResult errorResult = DialogResult.Abort;
Exception loadException;

public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
throw new Exception("My Exception");
}

/// <summary>
/// Shows the form as a modal dialog box with the currently active window
set as its owner.
/// </summary>
/// <returns>
/// One of the <see cref="T:System.Windows.Forms.DialogResult"></see> values.
/// </returns>
/// <exception cref="T:System.InvalidOperationException">The form being
shown is already visible.-or- The form being shown is disabled.-or- The form
being shown is not a top-level window.-or- The form being shown as a dialog
box is already a modal form.</exception>
/// <exception cref="T:System.InvalidOperationException">The current
process is not running in user interactive mode. For more information, see
<see
cref="P:System.Windows.Forms.SystemInformation.UserInteractive"></see>.</exception>
/// <exception cref="T:System.ArgumentException">The form specified in the
owner parameter is the same as the form being shown.</exception>
/// <PermissionSet><IPermission
class="System.Security.Permissions.EnvironmentPermission, mscorlib,
Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1" Unrestricted="true"/><IPermission
class="System.Security.Permissions.FileIOPermission, mscorlib,
Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1" Unrestricted="true"/><IPermission
class="System.Security.Permissions.SecurityPermission, mscorlib,
Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission
class="System.Security.Permissions.UIPermission, mscorlib,
Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1" Unrestricted="true"/><IPermission
class="System.Diagnostics.PerformanceCounterPermission, System,
Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1" Unrestricted="true"/></PermissionSet>
public DialogResult ShowDialog()
{
DialogResult result = base.ShowDialog();
if (result == errorResult)
{
if (loadException != null) throw loadException;
else throw new ApplicationException("An exception occured while showing
the dialog");
}
else return result;
}
/// <summary>
/// Raises the <see cref="E:System.Windows.Forms.Form.Load"></see> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs"></see> that