Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[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



Back to the top