PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Framework Forms RE: Exception not caught by a Try-Catch block

Reply

RE: Exception not caught by a Try-Catch block

 
Thread Tools Rate Thread
Old 05-01-2007, 10:34 AM   #1
=?Utf-8?B?Q2lhcmFuIE8nJycnRG9ubmVsbA==?=
Guest
 
Posts: n/a
Default RE: Exception not caught by a Try-Catch block


Sadly, this silly behaviour is by design:
http://support.microsoft.com/defaul...kb/324653/en-us

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://wannabedeveloper.spaces.live.com


"javiguti" wrote:

> 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??

  Reply With Quote
Old 05-01-2007, 11:51 AM   #2
=?Utf-8?B?amF2aWd1dGk=?=
Guest
 
Posts: n/a
Default RE: Exception not caught by a Try-Catch block

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

"Ciaran O''''Donnell" wrote:

> Sadly, this silly behaviour is by design:
> http://support.microsoft.com/defaul...kb/324653/en-us
>
> 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://wannabedeveloper.spaces.live.com
>
>
> "javiguti" wrote:
>
> > 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??

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off