SharpCrafters Forum – Is there a way to throw modified exception using OnException

SharpCrafters Support Forum

        


Is there a way to throw modified exception... Expand / Collapse
Author
Message
Posted 1/20/2010 12:45:51 AM


Community Member
Hi

Is there a way to modify original exception in OnException method??
My requirement is that we have an UI layer which uses a business layer to make calls to webservices/databases for getting data.
Business layer uses postsharp to have tracing and exception handling done at a common place.
UI layer only understands predefined buisness application exceptions so in business layer we map original exception thrown by databases/webservices to required application exception. Since this code for exception handling and mapping is repeated everywhere we wanted to leverage OnException method of OnMethodBoundaryAspect for doing it.
I have something like this

public override void OnException(MethodExecutionEventArgs eventArgs)
       {
           IContext currentContext = eventArgs.GetReadOnlyArgumentArray()[0] as IContext;
           if (currentContext != null)
           {
               // log the real exception
               currentContext.Logger.Write("Real Exception : ", eventArgs.Exception);
               // my mapping logic to application exception goes here
               // ..
               // now create a new application excpetion for UI layer
               eventArgs.Exception = new Exception("my new updated exception for UI layer");
           }
       }

but in UI layer I am not getting the updated exception, instead I get the real root exception which was thrown by database/webservice..
I looked in the generated code in my assembly and found that PostSharp does a throw of original exception.
Is there a way we can alter this flow behaviour and throw MethodExecutionEventArgs.Exception? I guess introducing a new flag in FlowBehaviour say FlowBehaviour.ThrowCustomException and use it in existing switch statement to throw MethodExecutionEventArgs.Exception will solve our issue.

Is there a way to achieve this in 1.5 RTM version of PostSharp?? Is there any alternative available which I can levergae to solve my problem??

~Prem
Post #3447
Posted 1/25/2010 7:09:58 AM


Gael Fraiteur

SharpCrafters
Hi Prem,

Thank you for your suggestion.

Currently, the only way to replace an exception is to throw an exception from the OnException advice, which has of course the side effect to replace the call stack and to add some stuff on the stack trace.

-gael
Post #3448
« Prev Topic | Next Topic »


All times are GMT +1:00, Time now is 11:46am

Powered By InstantForum.NET v4.1.4 © 2010
Execution: 0.054. 8 queries. Compression Disabled.