Re: Retrieve thrown exception in using (disposable) cleanup

From: Emil Astrom ("Emil)
Date: 03/22/05


Date: Tue, 22 Mar 2005 08:31:55 +0100

Alexander,

Thanks for the suggestion. I've thought about that too but the problem is
that I then explicitly have to catch all exceptions in the using scope (not
all of them are thrown by me). Doable, but it feels clumsy...

I hoped it might be possible to detect thrown exceptions in the same way
that Visual Studio does when debugging, but maybe I'll have to rethink my
design instead.

Cheers,

Emil

"Alexander Shirshov" <alexander@omnitalented.com> wrote in message
news:%23zN$dwoLFHA.2736@TK2MSFTNGP09.phx.gbl...
> Emil,
>
> It's generally suggested not to throw exceptions in Dispose:
>
> http://blogs.msdn.com/clyon/archive/2004/09/23/233464.aspx
>
> But if you insist... Can you save ErrorException in a field until Dispose
> method called?
>
> class MySession : : IDisposable
> {
> ErrorException thrownEx = null;
>
> Dispose()
> {
> if (mytransaction.IsOpen)
> throw new SessionException("Transaction not closed!", thrownEx);
> }
> }
>
> OtherFunc()
> {
> using (MySession session)
> {
> :
> thrownEx = new ErrorException("whoops!");
> throw thrownEx;
> session.Commit();
> }
> }
>
> HTH,
> Alexander
>
> "Emil Astrom" <emil_astrom(at)hotmail.com> wrote in message
> news:uU9o$CnLFHA.3760@TK2MSFTNGP12.phx.gbl...
>> Hi!
>>
>> I wonder if there's a way to retrieve information about thrown, not yet
>> handled exceptions. My situation is similar to the code below:
>>
>> class MySession : IDisposable
>> {
>> :
>> :
>> Dispose()
>> {
>> if (mytransaction.IsOpen)
>> throw new SessionException("Transaction not closed!", XXX);
>> }
>> }
>>
>> OtherFunc()
>> {
>> using (MySession session)
>> {
>> :
>> throw new ErrorException("whoops!");
>> session.Commit();
>> }
>> }
>>
>>
>> What I want to do is to have a Dispose() method that can include the
>> already thrown exception as an inner exception in its own exception that
>> it needs to throw if it's called prematurely. What I would like to have
>> in the example above is a thrown SessionException with the ErrorException
>> instance in the InnerException property. Can this be done?
>>
>> If not, then I can only see two options:
>> * not include ErrorException in my SessionException when I throw it, but
>> that means losing the original error
>> * not throw an exception in my Dispose method at all, but that means not
>> having any error messages for improper use of the MySession class, e.g.
>> forgetting to call Commit().
>>
>> Maybe someone have more (and better) alternatives? All suggestions are
>> greatly appreciated!
>>
>> Regards,
>>
>> Emil
>>
>
>



Relevant Pages

  • Re: Reflection problem
    ... I'm pretty sure that there are no exceptions being thrown while ... >> Incoming mail is certified Virus Free. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Configuration Error Not Helpful
    ... >What kind of error in the configuration section causes the exception to be ... schema validation, and any error that gets thrown from that gets this ... those exceptions never got displayed. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: sending over DO seems to make main process block?
    ... DO is one of the few places in Cocoa where exceptions are thrown that ... an exception may be thrown when the ... avoid deadlock. ... the sender expects to be called back from the receiver, ...
    (comp.sys.mac.programmer.help)
  • Re: throw in a finally
    ... > other well-documented texts available that point out this problem. ... > that the reasons behind it are the same as the reasons behind why ... Catch exceptions thrown in a finally even if the method declares they are ... Catch exceptions and just log them. ...
    (comp.lang.java.programmer)
  • Re: How much processor resources do try-catch blocks in C# use
    ... You are right...if no exceptions are thrown, ... Marc Butenko ... > I was discussing with someone how much processor resources try-catch ...
    (microsoft.public.vsnet.general)