Determine if an exception has been thrown

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ralf Jansen

    Determine if an exception has been thrown

    For logging purposes i want to determine if the current executing code is
    running in an ~exceptionhandl ing context~. I need no details about the exception
    just if an exception has been thrown and hasn't been handled yet.

    Following code should make clear what i mean and where i need that info.

    try
    {
    try
    {
    throw new Exception();
    }
    finally
    {
    // here i want to know if an exception has been thrown
    }
    }
    catch(Exception e)
    {
    // doSomething
    throw;
    }


    If i put a breakpoint inside the finally block i can inspect the Exception in
    the debugger through the '$Exception' pseudo variable. Is it possible to get
    that also in code?

    Thanks

    --
    Ralf Jansen

    deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
    Central Email Archiving The Easy Way - http://www.mailstore.com


  • =?Utf-8?B?S0g=?=

    #2
    RE: Determine if an exception has been thrown

    try
    {
    bool ex = false;
    try
    {
    throw new Exception();
    }
    catch(Exception ex)
    {
    ex = true;
    throw ex;
    }
    finally
    {
    // here i want to know if an exception has been thrown
    if (ex)
    {
    // an exception was thrown
    }
    }
    }
    catch(Exception e)
    {
    // doSomething
    throw;
    }

    "Ralf Jansen" wrote:
    For logging purposes i want to determine if the current executing code is
    running in an ~exceptionhandl ing context~. I need no details about the exception
    just if an exception has been thrown and hasn't been handled yet.
    >
    Following code should make clear what i mean and where i need that info.
    >
    try
    {
    try
    {
    throw new Exception();
    }
    finally
    {
    // here i want to know if an exception has been thrown
    }
    }
    catch(Exception e)
    {
    // doSomething
    throw;
    }
    >
    >
    If i put a breakpoint inside the finally block i can inspect the Exception in
    the debugger through the '$Exception' pseudo variable. Is it possible to get
    that also in code?
    >
    Thanks
    >
    --
    Ralf Jansen
    >
    deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
    Central Email Archiving The Easy Way - http://www.mailstore.com
    >
    >
    >

    Comment

    • Peter Duniho

      #3
      Re: Determine if an exception has been thrown

      On Fri, 17 Oct 2008 13:49:58 -0700, Ralf Jansen
      <ralf.jansen_no spam@deepinvent .comwrote:
      [...]
      If i put a breakpoint inside the finally block i can inspect the
      Exception in the debugger through the '$Exception' pseudo variable. Is
      it possible to get that also in code?
      I don't know the answer to that specific question. There's probably a
      way, though it could be very messy. But it seems to me that you could
      just track the state logically:

      bool fException = true;

      try
      {
      ...

      // very last thing in the try block:
      fException = false;
      }
      finally
      {
      if (fException)
      {
      // an exception was thrown
      }
      }

      That's the most literal-yet-simple solution to your question I see. That
      said, what's wrong with something like this:

      try
      {
      }
      catch
      {
      // log that an exception occurred

      throw;
      }
      finally
      {
      // do whatever
      }

      In other words, if you care that an exception was thrown, then just catch
      it, deal with it, and continue. Why make things complicated?

      Pete

      Comment

      • Ralf Jansen

        #4
        Re: Determine if an exception has been thrown

        Thanks, but in this case the finally Block will be before the Exception Block.
        As i said its for a logging purposes or to be more precise it is used for timing
        of a codeblock. The actual code is working via a the using statement and looks
        more like this

        try
        {
        using(LoggingSe rvice.Trace("Ac tivityID"))
        {
        DoSomethingTime Consuming();
        }
        }
        catch(Exception e)
        {
        // doSomething
        throw;
        }

        My LoggingService. Trace methods return an IDisposable object which does the
        timing. It starts timing in its constructor and logs the results in its Dispose
        method so i need to know if an exception happened in the Dispose method of that
        object.

        More ideas?

        KH schrieb:
        try
        {
        bool ex = false;
        try
        {
        throw new Exception();
        }
        catch(Exception ex)
        {
        ex = true;
        throw ex;
        }
        finally
        {
        // here i want to know if an exception has been thrown
        if (ex)
        {
        // an exception was thrown
        }
        }
        }
        catch(Exception e)
        {
        // doSomething
        throw;
        }
        >
        "Ralf Jansen" wrote:
        >
        >For logging purposes i want to determine if the current executing code is
        >running in an ~exceptionhandl ing context~. I need no details about the exception
        >just if an exception has been thrown and hasn't been handled yet.
        >>
        >Following code should make clear what i mean and where i need that info.
        >>
        >try
        >{
        > try
        > {
        > throw new Exception();
        > }
        > finally
        > {
        > // here i want to know if an exception has been thrown
        > }
        >}
        >catch(Exceptio n e)
        >{
        > // doSomething
        > throw;
        >}
        >>
        >>
        >If i put a breakpoint inside the finally block i can inspect the Exception in
        >the debugger through the '$Exception' pseudo variable. Is it possible to get
        >that also in code?
        >>
        >Thanks
        >>
        >--
        >Ralf Jansen
        >>
        >deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
        >Central Email Archiving The Easy Way - http://www.mailstore.com
        >>
        >>
        >>

        --
        Ralf Jansen

        deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
        Central Email Archiving The Easy Way - http://www.mailstore.com


        Comment

        • Ralf Jansen

          #5
          Re: Determine if an exception has been thrown

          In other words, if you care that an exception was thrown, then just
          catch it, deal with it, and continue. Why make things complicated?
          >
          Pete
          see my reply to KH.

          The complicted thing is that im trying to find a tight syntax for the logging,
          exception handling stuff that will not distract from the actual purpose of the
          code.


          --
          Ralf Jansen

          deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
          Central Email Archiving The Easy Way - http://www.mailstore.com


          Comment

          • =?Utf-8?B?S0g=?=

            #6
            Re: Determine if an exception has been thrown

            Thanks, but in this case the finally Block will be before the Exception
            Block.

            Helps if you post the actual problem you need help with. You know the
            "Exception block" (catch block?) can't come after the finally right?

            Like Peter said it sounds like you're making it way more complicated than it
            needs to be. using statement expands to a try/finally anyways - 'using' isn't
            any more efficient than writing it out yourself.


            "Ralf Jansen" wrote:
            Thanks, but in this case the finally Block will be before the Exception Block.
            As i said its for a logging purposes or to be more precise it is used for timing
            of a codeblock. The actual code is working via a the using statement and looks
            more like this
            >
            try
            {
            using(LoggingSe rvice.Trace("Ac tivityID"))
            {
            DoSomethingTime Consuming();
            }
            }
            catch(Exception e)
            {
            // doSomething
            throw;
            }
            >
            My LoggingService. Trace methods return an IDisposable object which does the
            timing. It starts timing in its constructor and logs the results in its Dispose
            method so i need to know if an exception happened in the Dispose method of that
            object.
            >
            More ideas?
            >
            KH schrieb:
            try
            {
            bool ex = false;
            try
            {
            throw new Exception();
            }
            catch(Exception ex)
            {
            ex = true;
            throw ex;
            }
            finally
            {
            // here i want to know if an exception has been thrown
            if (ex)
            {
            // an exception was thrown
            }
            }
            }
            catch(Exception e)
            {
            // doSomething
            throw;
            }

            "Ralf Jansen" wrote:
            For logging purposes i want to determine if the current executing code is
            running in an ~exceptionhandl ing context~. I need no details about the exception
            just if an exception has been thrown and hasn't been handled yet.
            >
            Following code should make clear what i mean and where i need that info.
            >
            try
            {
            try
            {
            throw new Exception();
            }
            finally
            {
            // here i want to know if an exception has been thrown
            }
            }
            catch(Exception e)
            {
            // doSomething
            throw;
            }
            >
            >
            If i put a breakpoint inside the finally block i can inspect the Exception in
            the debugger through the '$Exception' pseudo variable. Is it possible to get
            that also in code?
            >
            Thanks
            >
            --
            Ralf Jansen
            >
            deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
            Central Email Archiving The Easy Way - http://www.mailstore.com
            >
            >
            >
            >
            >
            --
            Ralf Jansen
            >
            deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
            Central Email Archiving The Easy Way - http://www.mailstore.com
            >
            >
            >

            Comment

            • Peter Duniho

              #7
              Re: Determine if an exception has been thrown

              On Fri, 17 Oct 2008 14:21:52 -0700, Ralf Jansen
              <ralf.jansen_no spam@deepinvent .comwrote:
              [...]
              My LoggingService. Trace methods return an IDisposable object which does
              the timing. It starts timing in its constructor and logs the results in
              its Dispose method so i need to know if an exception happened in the
              Dispose method of that object.
              What about this:

              using (LoggingService .TraceObject to =
              LoggingService. Trace("Activity ID"))
              {
              // do whatever...

              to.ExceptionOcc urred = false;
              }

              TraceObject could be the actual class (or base class) for the object
              implementing IDisposable. Or, if for some reason an inheritance isn't
              appropriate, it could be a simple one-property interface (call it
              IExceptionState or something like that). The ExceptionOccurr ed property
              could be write-only, basically providing input to the object implementing
              IDisposable that you got all the way through the code block without an
              exception.

              Another alternative that doesn't rely on IDisposable:

              LoggingService. Trace("Activity ID", delegate
              {
              // do whatever...
              });

              Where the LoggingService. Trace() method looks something like this:

              void Trace(string strID, Action action)
              {
              bool fException = true;

              try
              {
              action();

              fException = false;
              }
              finally
              {
              // do your logging, taking fException into account
              }
              }

              Ultimately, you're using IDisposable for something other than the purpose
              for which it was intended. If you insist on using IDisposable for this
              purpose, you may have to accept that this use isn't going to fit perfectly
              into the code, unless you are willing for your logging object to have some
              complicated, ugly hacks in it.

              Personally, I'd go with an approach that didn't use IDisposable.

              Pete

              Comment

              • Ralf Jansen

                #8
                Re: Determine if an exception has been thrown

                Ultimately, you're using IDisposable for something other than the
                purpose for which it was intended. If you insist on using IDisposable
                for this purpose, you may have to accept that this use isn't going to
                fit perfectly into the code, unless you are willing for your logging
                object to have some complicated, ugly hacks in it.
                Can't disagree here. Actually i found a solution in using
                Marshal.GetExce ptionPointers() but as you said its a hack. And presumably it
                wouldn't be the last one needed if i go further down that road.

                I like the delegate idea and will see if it fits .... on Monday ;)

                Thanks Peter

                --
                Ralf Jansen

                deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
                Central Email Archiving The Easy Way - http://www.mailstore.com


                Comment

                Working...