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 this work?



This variation will work:

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

ArithmeticException is already unchecked so the declare soft does not help
you much. In either case, the exception thrown at the join point (as
matched by the after throwing advice) will be an ArithmeticException.

If you actually want to *handle* the exception - rather than just execute
some behaviour after it is thrown, then you need to use around advice:

   Object around() : (execution (* HelloWorld.*(..)) && within(HelloWorld))
 {
   try {
     return proceed();
   } catch(Throwable t) {
     System.out.println("exception is " + t);
     return null;
   }
 }


-- Adrian.
adrian_colyer@xxxxxxxxxx





14 July 2004 14:30
To: "'aspectj-users@xxxxxxxxxxx'" <aspectj-users@xxxxxxxxxxx>
cc:
From: "Jerry Jalenak" <Jerry.Jalenak@xxxxxxxxxx>
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



Back to the top