|
-
May 12th, 2007, 06:12 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] [2005] Noticed This In Insert Snippets...
Try
Finally
End Try
How can this be used if there's no Catch block?
-
May 12th, 2007, 06:31 PM
#2
Re: [2005] Noticed This In Insert Snippets...
It is permissible to leave out the Catch, although its hard to see a good reason. This would allow some extra code to be run after an exception has occurred, that exception could then be caught by the ApplicationEvents UnhandledException handler.
I can't think of a good reason to do that other than perhaps passing a message to provide some information about in which sub/function the exception occurred.
Code:
Public OriginofException As String = String.Empty
Private Sub Nonsense()
Try
Dim s As New IO.StreamReader("DoesNotExist.txt")
Finally
OriginofException = "Nonsense"
End Try
End Sub
In the global exception handler, it could then be;
Code:
Namespace My
Partial Friend Class MyApplication
Private Sub MyApplication_UnhandledException( _
ByVal sender As Object, _
ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) _
Handles Me.UnhandledException
MessageBox.Show("Exception came from: " & Form1.OriginofException.ToString)
End Sub
End Class
End Namespace
Last edited by Bulldog; May 12th, 2007 at 06:42 PM.
-
May 12th, 2007, 06:51 PM
#3
Thread Starter
Frenzied Member
Re: [2005] Noticed This In Insert Snippets...
Interesting,
Private Sub Nonsense() seems to be the answer.
Thanks
-
May 13th, 2007, 12:59 AM
#4
Re: [2005] Noticed This In Insert Snippets...
Q. What's a Try block for?
A. To attempt to execute code that may throw an exception.
Q. What's a Catch block for?
A. To catch an exception and deal with it.
Q. What's a Finally block for?
A. To perform cleanup that must be done whether an exception is thrown or not.
Q. What if you need to perform cleanup but you don't want to catch any exceptions?
A. You have a Try block with a Finally block but no Catch block. That way the required cleanup is performed but amy exceptions will bubble up to the caller to be caught there, which is a completely legitimate design.
-
May 13th, 2007, 04:45 AM
#5
Thread Starter
Frenzied Member
Re: [2005] Noticed This In Insert Snippets...
So we would be trying to catch the error at a higher level on purpose. And MS has given us the flexibility to do that.
I understand.
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|