PDA

View Full Version : Error handling


mgartman
11-23-2004, 02:13 AM
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

Scott
11-23-2004, 04:02 AM
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

mgartman
11-23-2004, 04:16 AM
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

bruce
11-23-2004, 08:29 AM
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 (http://www.DiscountASP.NET)