PDA

View Full Version : ThrowsAdvice Query


Ico
05-04-2007, 12:43 PM
Hi,

I am trying to implement a ThrowsAdvice and am getting some strange behaviour that I'm not sure is correct. I have wired up my ThrowsAdvice and it is being triggered correctly, but the problem I am having is that when an exception is thrown from within any of my AfterThrowing methods, only the baseException gets thrown. I have had a look in the Spring code and believe the following method inside ThrowsAdviceInterceptor is responsible.

private void InvokeHandlerMethod(
IMethodInvocation invocation, Exception triggeringException, MethodInfo handlerMethod)
{
object[] handlerArgs;
if (handlerMethod.GetParameters().Length == 1)
{
handlerArgs = new object[] {triggeringException};
}
else
{
handlerArgs = new object[] {invocation.Method, invocation.Arguments, invocation.This, triggeringException};
}
try
{
handlerMethod.Invoke(this.throwsAdvice, handlerArgs);
}
catch (TargetInvocationException ex)
{
throw ex.GetBaseException();
}
}

So finally to my question, inside the catch(TargetInvocationException ex) the baseException is being thrown, this seems wrong to me, either the whole exception should be thrown or just the innerexception thereby discarding the targetinvocationexception. Is this a bug, or are my expectations of how ThrowsAdvice work just plain wrong.

Any guidence on this would be much appreciated

thanks

Jim

Bruno Baia
05-04-2007, 01:38 PM
Hi,

I'll investigate, looks weird to me too...
which version are you using ?

Bruno

Ico
05-04-2007, 02:21 PM
Hi, thanks for the quick response

Embarrassingly I'm not sure of the version I'm using ( I think it was a nightly build from the beginning of this year ) . We are planning to upgrade to the latest version but it breaks our code and so we haven't done it yet. However I've looked at the code in the latest nightly build and the ThrowsAdviceInterceptor is unchanged.
For the moment I have written my own interceptor that replicates the ThrowsAdvice behaviour for my own purposes. Really I'm just interested to find out if this is a bug or if not the reasoning behind the behaviour.

Thanks again

Jim

Bruno Baia
05-04-2007, 02:23 PM
I've changed it to throw InnerException.

This only happens if an exception is thrown by IAfterThrowingAdive.AfterThrowing method, and it seems you are the first getting this.

What you need to know is that you don't need to throw the exception in AfterThrowing implementation, because Spring will do it.

public void AfterThrowing(Exception ex)
{
Console.Error.WriteLine(
String.Format("Advised method threw this exception : {0}", ex.Message));
// throw ex; // Not needed, done by Spring;
}

Maybe you know that and you are doing something specific.


Thanks for catching this,
Bruno

Ico
05-04-2007, 02:39 PM
Great, thanks Bruno