Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] exception handling within several methods

Yeah that is the library i'm missing, aspectjrt.jar, it says the runtime.jar isn't in the plugin folder but it actually is in there. And thanks a bunch for clearing up my aspect, that helps a bunch
________________________________________
From: aspectj-users-bounces@xxxxxxxxxxx [aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Andy Clement [andrew.clement@xxxxxxxxx]
Sent: Thursday, December 02, 2010 1:10 PM
To: aspectj-users@xxxxxxxxxxx
Subject: Re: [aspectj-users] exception handling within several methods

On 2 December 2010 10:24, Johnson, Trace <trace.johnson@xxxxxxx> wrote:
> Yeah I did the run/as aspectj/javaapplication and it comes up with 2 errors, one is main doesn't show up and the second is that it is missing an aspectj library that is in the plugins folder and I don't know why it can't find it. I'm running this on windows as well in eclipse and it complains if Exception isn't capitalized. Atleast tell me this, does the aspect catch the exception, is it coded correctly because without being able to test it I have no idea if it is correctly coded or not.

Which aspect library is considered missing? aspectjrt.jar?  Are you
maybe picking up an old launch configuration that isn't configured
correctly?

As for whether aspect works.  I'm not 100% sure what you are trying to
achieve, but it doesn't look quite right:

1. the aspect softens IOExceptions coming from login().  As far as I
can see there are no IOExceptions coming from login?  If the
sc.nextInt() call exits via exception it is an unchecked exception
(one of InputMismatchException, NoSuchElementException or
IllegalStateException).  This makes the declare soft redundant.

2. From the around advice you seem to want to let the AspectJ handle
the problem of an exception coming from login().  We can do this if
you remove the try...catch clause around the sc.nextInt() call.  That
would ensure that if login exits via an exception, the aspect will
print 'aspect caught: only numbers are allowed'.  Unfortunately it
will print it as the login() method is finished, effectively skipping
login (since it will skip over the 'if (x!=y)' check).

What I think you want to say is:

int around(): call(int java.util.Scanner.nextInt()) {
  try {
    return proceed();
  } catch (Exception e) {
    System.out.println("aspect caught: only numbers are allowed\n");
    return -1;
  }
 }

and remove your declare soft, and you can then remove the 'catch
(Exception e)' entries around your calls to sc.nextInt throughout the
code.

With that in place all the calls to nextInt() in your atm type are
then guarded by the aspect and should any of them throw the exception
you get the aspect message and it returns '-1'.

You could probably achieve a similar thing with a helper method in atm
and no aspect.  Replace your calls to sc.nextInt() with getInt(sc),
then in atm:

public int getInt(Scanner sc) {
  try {
    return sc.nextInt();
  } catch (Exception e) {
    return -1;
  }
}

hope that is of some use,
Andy

>
> ________________________________________
> From: aspectj-users-bounces@xxxxxxxxxxx [aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Andy Clement [andrew.clement@xxxxxxxxx]
> Sent: Thursday, December 02, 2010 12:10 PM
> To: aspectj-users@xxxxxxxxxxx
> Subject: Re: [aspectj-users] exception handling within several methods
>
> Hi,
>
> On 1 December 2010 14:52, Johnson, Trace <trace.johnson@xxxxxxx> wrote:
>> I'm trying to figure out to change the exceptions for the scanner in several different methods into one aspect, I have done several things but a few times I've tried to run it I'm getting a no main error and no aspectj runtime error here is the code:
>
> I created the files atm.java and exception.aj containing your code, I
> can run atm just fine via the context menus (right clicking in the
> editor): RunAs>JavaApplication or RunAs>AspectJ/JavaApplication.  How
> are you launching it?  I presume you are always attempting to launch
> the code when there are no errors in it?
>
> I am on windows which is rather indifferent about upper/lowercasing in
> places.  It is unusual to have type names starting with a lower case
> letter, and if you modified the aspect from exception to Exception I
> can imagine that causing all kinds of issues due to clashing with
> java.lang.Exception.  We don't knowingly have a bug related to
> handling upper/lower case - I'm just trying to think about what is
> unusual in your program.
>
> Andy
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top