Exception Handling Question
Exception Handling Question
am 24.01.2008 21:18:07 von Gz
Suppose I have the below code:
FileStream fs1 = new FileStream(...);
FileStream fs2 = new FileStream(...);
try
{
fs1.Write(...)
fs2.Write(...)
}
finally
{
if(fs1!=null)
fs1.Dispose();
if(fs2!=null)
fs2.Dispose();
}
My question is what happens if fs1.Dispose() throws an exception.
Maybe the disk is bad and it cannot successfully write the last piece
of buffer to it. If that happens, then:
1) Do I consider fs1 disposed?
2) Is it correct that fs2 is not Disposed?
In order for this to work, I have to write:
finally
{
try{
if(fs1!=null)
fs1.Dispose();
}
finally
{
if(fs2!=null)
fs2.Dispose();
}
}
This is very ugly, and incorrect. What happens if both fs1.Dispose()
and fs2.Dispose() throw exceptions?
Thanks,
gz
Re: Exception Handling Question
am 24.01.2008 22:57:57 von skeet
gz wrote:
> Suppose I have the below code:
>
> FileStream fs1 = new FileStream(...);
> FileStream fs2 = new FileStream(...);
>
> try
> {
> fs1.Write(...)
> fs2.Write(...)
> }
> finally
> {
> if(fs1!=null)
> fs1.Dispose();
>
> if(fs2!=null)
> fs2.Dispose();
> }
>
> My question is what happens if fs1.Dispose() throws an exception.
Then fs2.Dispose doesn't get called. This is a good reason to use the
using statement:
using (FileStream fs1 = new FileStream(...))
using (FileStream fs2 = new FileStream(...))
{
}
Nested try/finally, and all is well.
(The exception would still get propagated, of course.)
--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Re: Exception Handling Question
am 24.01.2008 23:37:50 von Gz
On Jan 24, 3:57=A0pm, Jon Skeet [C# MVP] wrote:
> gz wrote:
> > Suppose I have the below code:
>
> > FileStream fs1 =3D new FileStream(...);
> > FileStream fs2 =3D new FileStream(...);
>
> > try
> > {
> > =A0 =A0fs1.Write(...)
> > =A0 =A0fs2.Write(...)
> > }
> > finally
> > {
> > =A0 =A0 if(fs1!=3Dnull)
> > =A0 =A0 =A0 =A0 =A0 fs1.Dispose();
>
> > =A0 =A0if(fs2!=3Dnull)
> > =A0 =A0 =A0 =A0 =A0fs2.Dispose();
> > }
>
> > My question is what happens if fs1.Dispose() throws an exception.
>
> Then fs2.Dispose doesn't get called. This is a good reason to use the
> using statement:
>
> using (FileStream fs1 =3D new FileStream(...))
> using (FileStream fs2 =3D new FileStream(...))
> {
>
> }
>
> Nested try/finally, and all is well.
>
> (The exception would still get propagated, of course.)
>
> --
> Jon Skeet - http://www.pobox.com/~skeet=A0 Blog:http://ww=
w.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk- Hide q=
uoted text -
>
> - Show quoted text -
Thanks for the response. I still have two questions:
1. If f1.Dispose() throws an exception, can I consider it is Disposed.
If not, what should I do?
2. If both f1.Dispose() and f2.Dispose() throw exceptions, which one
gets propagated?
Re: Exception Handling Question
am 24.01.2008 23:43:24 von skeet
gz wrote:
> Thanks for the response. I still have two questions:
>
> 1. If f1.Dispose() throws an exception, can I consider it is Disposed.
> If not, what should I do?
I certainly would.
> 2. If both f1.Dispose() and f2.Dispose() throw exceptions, which one
> gets propagated?
Whichever throws last. The earlier exception (unfortunately) gets lost.
--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
RE: Exception Handling Question
am 24.01.2008 23:54:01 von FamilyTreeMike
I had never really thought about that before. If you look in MSDN, the
dispose method does not list any possible exceptions for the Dispose method,
as it would for known, throwable exceptions in other classes and methods.
"gz" wrote:
> Suppose I have the below code:
>
> FileStream fs1 = new FileStream(...);
> FileStream fs2 = new FileStream(...);
>
> try
> {
> fs1.Write(...)
> fs2.Write(...)
> }
> finally
> {
> if(fs1!=null)
> fs1.Dispose();
>
> if(fs2!=null)
> fs2.Dispose();
> }
>
> My question is what happens if fs1.Dispose() throws an exception.
> Maybe the disk is bad and it cannot successfully write the last piece
> of buffer to it. If that happens, then:
>
> 1) Do I consider fs1 disposed?
> 2) Is it correct that fs2 is not Disposed?
>
> In order for this to work, I have to write:
>
> finally
> {
> try{
> if(fs1!=null)
> fs1.Dispose();
> }
> finally
> {
> if(fs2!=null)
> fs2.Dispose();
> }
> }
>
> This is very ugly, and incorrect. What happens if both fs1.Dispose()
> and fs2.Dispose() throw exceptions?
>
> Thanks,
> gz
>
>
Re: Exception Handling Question
am 26.01.2008 06:01:39 von Gz
I see. Thanks!
On Jan 24, 4:43=A0pm, Jon Skeet [C# MVP] wrote:
> gz wrote:
>
>
>
> > Thanks for the response. I still have two questions:
>
> > 1. If f1.Dispose() throws an exception, can I consider it is Disposed.
> > If not, what should I do?
>
> I certainly would.
>
> > 2. If both f1.Dispose() and f2.Dispose() throw exceptions, which one
> > gets propagated?
>
> Whichever throws last. The earlier exception (unfortunately) gets lost.
>
> --
> Jon Skeet - http://www.pobox.com/~skeet=A0 Blog:http://ww=
w.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk