I have the following code:
try{
using (StreamReader reader = new StreamReader(...), Encoding.ASCII)){
// Code that can throw an exception
}
}catch (Exception error){
// Display error...
}
What will happen to the StreamReader
in case there is an exception thrown from within the using
block?
Should I add a finally
clause where I close the stream?
The StreamReader will be disposed automatically by the using, as it's essentially a nested try/finally:
try{
StreamReader reader = new StreamReader(...), Encoding.ASCII);
try {
// Code that can throw an exception
} finally {
reader.Dispose();
}
} catch (Exception error) {
// Display error...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With