[aosd-discuss] exceptions thrown from advice

Dean Wampler dean at aspectprogramming.com
Mon Apr 7 10:09:58 EDT 2008


The short answer is that it works just like Java.

For the long answer, first, your SomeException would have to be an  
unchecked exception (subtype of RuntimeException). AspectJ respects  
the rules of Java concerning checked vs. unchecked exceptions. If you  
tried to throw a checked exception, the compiler wouldn't allow it,  
because the method you are advising doesn't handle it or declare that  
it throws it (unless you are throwing an exception that the method  
does handle or throw, of course...).

There has been a long debate on checked vs. unchecked exceptions,  
which I won't rehash here. (But I will say that checked exceptions are  
mostly a bad idea, IMHO ... ;)

So, assuming your advice is throwing an unchecked exception, it will  
get caught by the first catch clause in the stack that is "wide  
enough" to catch a RuntimeException. Otherwise, the program will  
crash, just as in normal Java.

For this analysis, you could imagine that you subclassed "S" and  
overrode "something" in the subclass. What if the new "something"  
throws "SomeException"? The same analysis would apply.

I hope that makes sense.

Dean

On Apr 7, 2008, at 4:20 AM, Florin-Alexandru Bratu wrote:

> Hello everyone!
>
> This is more of a general question regarding AOP. If I have defined an
> advice that throws an exception, where is that exception handled?
>
> For instance: say that I have a method:
>
> class S{
> public void something() {
>   // something
> }
> }
>
> and that I have an advice that triggers whenever the method starts
> executing. The advice is
>
> class Advice {
> public void  doOnSomething() throws  SomeException  {
>       throw new SomeException();
> }
> }
>
> (suppose the advice is properly configured)
>
> Now; if I do something like this:
>
> class Main {
>   public void other()
>         // is this necessary?
>        // throws SomeException
>   {
>          new S().something();
>   }
> }
>
>
> My question is: The call new S().something(); triggers an exception in
> the method other() ?
>
> Thank you,
> Florin
>
> _______________________________________________
> discuss mailing list    -    discuss at aosd.net
>
> To unsubscribe and change options, go to:
> http://aosd.net/mailman/listinfo/discuss_aosd.net
>
> Check out the AOSD.net Wiki: http://aosd.net/wiki

Dean Wampler, Ph.D.
dean at objectmentor.com
http://www.objectmentor.com
See also:
http://www.aspectprogramming.com  AOP advocacy site
http://aquarium.rubyforge.org     AOP for Ruby
http://www.contract4j.org         Design by Contract for Java5



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://aosd.net/pipermail/discuss_aosd.net/attachments/20080407/7e87475b/attachment.html 


More information about the discuss mailing list