CodeSmith Community
Your Code. Your Way. Faster!

Exception handling with ServiceResult.ExceptionList

Latest post 08-27-2007 10:59 AM by GRAW. 5 replies.
  • 06-07-2007 5:10 PM

    • ataylor
    • Not Ranked
    • Joined on 05-09-2007
    • Posts 1
    • Points 95

    Exception handling with ServiceResult.ExceptionList

    I am executing the processors with AbortOnFailure = true, and I want to rethrow the exception(s) that has been caught in the ServiceBaseCore Process() method. There will only be one error in the ExceptionList. However, when I rethrow the exception stored in the ExceptionsList, .NET picks it up as a new error and I lose the stack trace, source and targetid of the original exception.  InnerException and GetBaseException()  do not apply here, because the exception chain has been interrupted when the ServiceBaseCore.Process() catches any exceptions.  Does anyone have any suggestions for bubbling up the original exception?  My code looks like this:

    //Run validation processors

    ServiceResult result = Execute (true);

    if ( result.ExceptionList.Count > 0 )

    {

    foreach ( Exception exc in result.ExceptionList.Values )

    {

    string message = string.Format ( "Message: {0} \r\nSource: {1} \r\nStackTrace: {2}\r\nTargetSite: ", exc.Message, exc.Source, exc.StackTrace, exc.TargetSite );

    throw new Exception ( message );

    }

    }

     

    • Post Points: 95
  • 06-11-2007 11:15 AM In reply to

    Re: Exception handling with ServiceResult.ExceptionList

    If you have abortOnFailure set to true, there will always be at most only 1 exception there.

    so just try to rethrow the same exception.

    if (result.ExceptionList.Count > 0)
        throw result.ExceptionList.Values[0] as Exception;


    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 35
  • 06-17-2007 5:55 PM In reply to

    • skbach
    • Top 75 Contributor
    • Joined on 01-18-2007
    • Vancouver, British Columbia
    • Posts 58
    • Points 1,500

    Re: Exception handling with ServiceResult.ExceptionList

     Robert,

     That code results in the following error:

    Error    2    Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.Dictionary<TransitApp.Services.ProcessorBase,System.Exception>.ValueCollection'

    I'm not using the nightly builds and this is the version from back in January, so maybe yours works?

    Thanks.

     

    Scott Klarenbach PointyHat Software www.pointyhat.ca _______________________________________ To iterate is human; to recurse, divine
    • Post Points: 5
  • 08-25-2007 6:27 AM In reply to

    Re: Exception handling with ServiceResult.ExceptionList

    skbach, I am using 2.0.1 version of framework and get same error when doing what swin suggests (but it might work in later versions). Have you tried the following?

    ServiceResult result = Execute (true);

    if ( result.ExceptionList.Count > 0 )

    {

    foreach ( Exception exc in result.ExceptionList.Values )

    {

    throw exc;

    }

    }

    This should just re throw the first exception, and as swin says the only exception in list because you are aborting on failure, in the list.

    I have just applied this approach in some code I am working on and it does indeed rethrow the error. 

     

    • Post Points: 35
  • 08-25-2007 1:38 PM In reply to

    • skbach
    • Top 75 Contributor
    • Joined on 01-18-2007
    • Vancouver, British Columbia
    • Posts 58
    • Points 1,500

    Re: Exception handling with ServiceResult.ExceptionList

     Awesome SBB...I'll give that a try...

     sk
     

    Scott Klarenbach PointyHat Software www.pointyhat.ca _______________________________________ To iterate is human; to recurse, divine
    • Post Points: 5
  • 08-27-2007 10:59 AM In reply to

    • GRAW
    • Top 25 Contributor
    • Joined on 06-23-2006
    • Posts 157
    • Points 4,560

    Re: Exception handling with ServiceResult.ExceptionList

    As far as I know, the only way to preserve the original stack information is to use the 'throw' statement by itself in a catch block, any other use of the throw statement, i.e. throw new XXXException() will figure as though the exception occurred at the location of the throw statement in code.

    However, i think you can achieve what you need, because you do have the original exception in the ExceptionList.

    Try something like this:

    foreach ( Exception exc in result.ExceptionList.Values )

    {

    string message = string.Format ( "Exception rethrown {0}.", exc.ToString() );

    throw new Exception ( message, exc );

    }

    This should preserve the original Exception, with all its info. 

    "Small is the number of them that see with their own eyes, and feel with their own hearts" Albert Einstein
    • Post Points: 5
Page 1 of 1 (6 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems