Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exception handling question....

Status
Not open for further replies.

tektipsFriend

Programmer
Dec 2, 2006
7
US
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
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. ;-)
 
TekTipsFriend, you shouldnt need "error handling" around the creation of an object. If there are instances where the object doesnt get created then the writer of the class needs to re-evalute their definition of "good enough for production"

Any cleanup for the try block should be done either at the end of the try, in the catch, or as a last ditch, in the finally.

An object that is no longer needed should be .disposed() at the end of the routine that created it.

-The answer to your problem may not be the answer to your question.
 
What Qik3Coder says is true. But there is a way around it.

Code:
dim loCmd as SQLCommand = nothing

    Try
        loCmd = new SQLCommand()
        loCmd.ExecuteNonQuery()
    Catch ex as Exception
        Messagebox.show locmd.commandtext
    Finally
        locmd.dispose
    End Try

Not that I see any reason why the command won't instantiate. But perhaps I'm missing something.

Christiaan Baes
Belgium

"My new site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top