Question: Try,Catch,Finally

Discussion in 'ASP .Net' started by VB Programmer, Aug 7, 2003.

  1. I usually put my declarations for db connections, datasets, datareaders,
    etc... above the Try portion. Inside the Try is where I open my connection,
    executereader, etc... In the Catch portion I usually "clean it up", with
    closing the datareaders, connections, etc... Is this the best way to do it?

    The reason I ask is that if something fails (in the Try) and it didn't, get
    to open the db connection yet, it will jump to Finally and that will give me
    an error because I cannot close a db connection that is not open. Is there
    another way around this?
    VB Programmer, Aug 7, 2003
    #1
    1. Advertising

  2. The solution is to write a routine that "safely" closes a Connection, and
    use that in the Finally block. Here's an example:

    Shared Function CloseConn(ByRef objConn As SqlConnection, _
    ByRef objCommand As SqlCommand) As Boolean
    Try
    If Not IsNothing(objConn) Then objConn.Dispose() ' Calls
    Close() if objConn is Opened
    If Not IsNothing(objCommand) Then objCommand.Dispose()
    Return True
    Catch E As Exception
    HandleError(E) ' Our implementation of this writes a message
    to the Event Log
    Return False
    End Try
    End Function

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    http://www.takempis.com
    Complex things are made up of
    lots of simple things.

    "VB Programmer" <> wrote in message
    news:...
    > I usually put my declarations for db connections, datasets, datareaders,
    > etc... above the Try portion. Inside the Try is where I open my

    connection,
    > executereader, etc... In the Catch portion I usually "clean it up", with
    > closing the datareaders, connections, etc... Is this the best way to do

    it?
    >
    > The reason I ask is that if something fails (in the Try) and it didn't,

    get
    > to open the db connection yet, it will jump to Finally and that will give

    me
    > an error because I cannot close a db connection that is not open. Is

    there
    > another way around this?
    >
    >
    Kevin Spencer, Aug 7, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    The problem with Try Catch Finally...

    VB Programmer, Aug 12, 2003, in forum: ASP .Net
    Replies:
    23
    Views:
    764
    Chad Myers
    Aug 15, 2003
  2. Ralph Krausse

    ALL 'try/catch/finally' NOT created equal?

    Ralph Krausse, Aug 20, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    408
  3. hansiman

    try catch finally

    hansiman, Jul 14, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    480
    Sean M
    Jul 14, 2005
  4. Alfredo

    Try Catch and Finally

    Alfredo, Aug 11, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    1,359
    Kevin Spencer
    Aug 11, 2005
  5. David Lozzi

    Try...Catch...Finally not firing finally?

    David Lozzi, Apr 23, 2007, in forum: ASP .Net
    Replies:
    12
    Views:
    783
    Alvin Bruney [MVP]
    May 11, 2007
Loading...

Share This Page