tektipsFriend
Programmer
I'm a little confused as to how to put my question into words but I'll do my best. ;-)
I've noticed that if I declare an object inside a try catch block I cant call that object in either the catch or finally statement. However I'd like to be able to include some information from the object in my exception handling statement in my catch branch.
So I've had to instantiate my objects outside of the try catch in order for me to be able to include the object in my catch branch. What concerns me about this is that I've instantiated an object outside of the try...catch so what happens if there is an error in the instantiation? I wont have any error handling... right?
For example
The other thing that concerns me is that I'm instantiating objects that I wont need or use, in some cases. Should I put a Finally branch on the Try...Catch and call the dispose method of the object?
Like so...
Thanks, I'm grateful for any advice. ;-)
I've noticed that if I declare an object inside a try catch block I cant call that object in either the catch or finally statement. However I'd like to be able to include some information from the object in my exception handling statement in my catch branch.
So I've had to instantiate my objects outside of the try catch in order for me to be able to include the object in my catch branch. What concerns me about this is that I've instantiated an object outside of the try...catch so what happens if there is an error in the instantiation? I wont have any error handling... right?
For example
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dim loCmd as new SQLCommand
Try
loCmd.ExecuteNonQuery()
Catch ex as Exception
Messagebox.show locmd.commandtext
End Try
End Sub
The other thing that concerns me is that I'm instantiating objects that I wont need or use, in some cases. Should I put a Finally branch on the Try...Catch and call the dispose method of the object?
Like so...
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dim loCmd as new SQLCommand
Try
loCmd.ExecuteNonQuery()
Catch ex as Exception
Messagebox.show locmd.commandtext
Finally
locmd.dispose
End Try
End Sub
Thanks, I'm grateful for any advice. ;-)