Error handling

Discussion in 'Databases' started by mgartman, Nov 23, 2004.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Hello,

    I've been working with opening a datareader and creating error handling for it. Is it enough to just open the Datareader in a Try block and not close it in the finally?

    I can't seem to find a way to close the reader in thefinally block, if I create the Datareader in the try block then it is out of scope in the finally. If I create the datareader out of the error handling, VS throws and error saying that I can't close an unassigned variable.

    Will the datareader close automatically if there is an error before it reaches it's .Close(); in the try block?

    Is there a better way to do this?

    Thanks!

    Matt
     
  2. Not sure without seing your code, but I believe you should be able to declare the data reader before the start of the try block, then actually create it in the try block.

    For example:
    dim dr as OleDbDataReader

    try
    dr = creation code here
    ...
    catch
    ....
    finally
    dr.close
    ...
    end try
     
  3. Thanks for the reply Scott. The code that you sent I belive will work in VB, but in C# you will get a "Use of unassigned local variable" compiler error.

    I've tried a couple of ways now and I can't seem to see any good way to close it in the finally. So currently I'm just closing it in the TRY block. If it errors out, hopfully it isn't leaving that Datareader open.

    Matt
     
  4. Bruce

    Bruce DiscountASP.NET Staff

    Try block should run fine w/ C#

    I got this out of my quick referenece book.

    Try {

    .....

    }
    Catch (ExecptionA ex) {
    ..... //react to exception type ExceptionA
    }
    finally {

    }

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page