Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] [newbie] Exception handling - why doesn't thi s work?

How about:

public aspect ExceptionHandling {
after() throwing(Throwable ex) : execution(* *.*(..)) && !within(ExceptionHandling) {
      ... log "ex"
   }
}

This will leave the normal exception propagation unaffected.

-Ramnivas
===
Ramnivas Laddad,
Author, AspectJ in Action
http://ramnivas.com

Jerry Jalenak wrote:

Continuing this thought....

Is it possible to write an pointcut that will catch both checked and
unchecked exceptions, but allow them to continue once the advice is
complete?  I working on a generic tracing aspect and want to be able to
trace exceptions if and when they are thrown, but then allow the exception
to continue and potentially be caught by another aspect that will actually
handle the exception.

Any thoughts?

Thanks...

Jerry Jalenak
Development Manager, Web Publishing
LabOne, Inc.
10101 Renner Blvd.
Lenexa, KS  66219
(913) 577-1496

jerry.jalenak@xxxxxxxxxx


-----Original Message-----
From: Ron Bodkin [mailto:rbodkin@xxxxxxxxxxxxxx]
Sent: Wednesday, July 14, 2004 10:37 AM
To: aspectj-users@xxxxxxxxxxx
Subject: Re: [aspectj-users] [newbie] Exception handling - why doesn't
this work?


Hi Jerry,

Your code will work if you remove the throws clause from the call pointcut descriptor, i.e.,
change to
   declare soft : ArithmeticException :
       call(* *.*(..)) && within(HelloWorld);

The expression call(* *.*(..) throws ArithmeticException) matches method call join points where the method being called is statically declared to throw exceptions of type ArithmeticException

But ArithmeticException is an unchecked exception. And the JDK method calls that throw it do not declare it as part of their signature; it is not required to say a method throws unchecked exceptions, although it is allowed. I.e., the following are equivalent and both are valid Java code:

void foo() throws RuntimeException {
   throw new RuntimeException();
}

void foo() {
   throw new RuntimeException();
}

However call(* *(..) throws RuntimeException) would only match calls to the first version.

But why are you softening an exception that is already unchecked? Why not leave out the declare soft and just use after throwing advice on the ArithmeticException, e.g.,

aspect ExceptionHandler
{
   after() throwing(ArithmeticException ex) :
       call (* *(..)) && within(HelloWorld)
   {
       System.out.println("exception is " + ex.getMessage());
   }
}

Ron Bodkin
Chief Technology Officer
New Aspects of Software
o: (415) 824-4690
m: (415) 509-2895

------------Original Message------------
From: "Jerry Jalenak" <Jerry.Jalenak@xxxxxxxxxx>
To: "'aspectj-users@xxxxxxxxxxx'" <aspectj-users@xxxxxxxxxxx>
Date: Wed, Jul-14-2004 6:27 AM
Subject: [aspectj-users] [newbie] Exception handling - why
doesn't this work?
Hi all,

I'm in the processing of learning AspectJ, and am excited about the
possibilities it offers - one of which is the abililty to
abstract out
exception handling into a common aspect. I've written up a
real simple
java
program and aspect to catch a divide by zero exception, but I can't seem to
get it to work.  Here's the code :

public class HelloWorld
{

	public static void main(String[] args)
	{
		helloWorld();
	}
	
	private static void helloWorld()
	{
		int a = 10 / 0;
	}
}

aspect ExceptionHandler
{
	declare soft : ArithmeticException :
		call(* *.*(..) throws ArithmeticException) &&
within(HelloWorld);
	
	after() throwing(SoftException ex) :
		(execution (* HelloWorld.*(..)) && within(HelloWorld))
	{
		System.out.println("exception is " +
ex.getWrappedThrowable().getMessage());
	}
}

When I run this (under Eclipse) I get the following messages in the console
window :

java.lang.ArithmeticException: / by zero
	at org.labone.HelloWorld.helloWorld(HelloWorld.java:20)
	at org.labone.HelloWorld.main(HelloWorld.java:15)
Exception in thread "main" It appears that the aspect is never being invoked. What am I doing wrong?

Thanks!

Jerry Jalenak
Development Manager, Web Publishing
LabOne, Inc.
10101 Renner Blvd.
Lenexa, KS  66219
(913) 577-1496

jerry.jalenak@xxxxxxxxxx


This transmission (and any information attached to it) may be confidential and is intended solely for the use of the individual or entity
to which it
is
addressed. If you are not the intended recipient or the person responsible for delivering the transmission to the intended recipient, be
advised that
you
have received this transmission in error and that any use, dissemination, forwarding, printing, or copying of this information is strictly prohibited. If you have received this transmission in error, please immediately notify LabOne at the following email address: securityincidentreporting@xxxxxxxxxx

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users


_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users


This transmission (and any information attached to it) may be confidential and is intended solely for the use of the individual or entity to which it is addressed. If you are not the intended recipient or the person responsible for delivering the transmission to the intended recipient, be advised that you have received this transmission in error and that any use, dissemination, forwarding, printing, or copying of this information is strictly prohibited. If you have received this transmission in error, please immediately notify LabOne at the following email address: securityincidentreporting@xxxxxxxxxx

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users




Back to the top