Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Exception dilemma

Hello Wim,

ah, yes, you are correct. For JUnit-testing it would be the better way. But I want to use it not only for testing with JUnit but also for web testing: how reacts our web application if an exception was thrown? Do we see an stacktrace on the web page (not good) or does the user get a helpful message?

For this reason I plan to write a general aspect where I can control via JMX if an exception will be thrown with the next action or not.

regards,
Oliver


Wim Deblauwe schrieb:
Would it not be easier, if this is for testing only, to use JMock? You can say in JMock 'make this method throw this exception when I call it'.

regards,

Wim

2009/3/5 Oliver Böhm <boehm@xxxxxxxxxx <mailto:boehm@xxxxxxxxxx>>

    Hi Thomas,

    yes, that's exactly what I want - I tried your code and it works. I
    had a little problem to understand the code but I think the trick
    here is to use the constructor of the inner class to throw the
    desired exception.

    What I don't understand is the $ sign before the method name
    ($throw) but I guess this is syntax for inner classes.

    thanx for your tip
    Oliver



    Thomas Richard Darimont schrieb:

        Hi Oliver,

        so you want to throw an exception after some specific Methods return

        How about this:
        (Just a quick hack...)

        Aspect:
        [code]
        package de.tutorials.training.aspects;

        import java.io.IOException;

        public aspect ExampleAspect {
          pointcut applicationCode(): execution(* *..*(..)) && !
        within(ExampleAspect);
         //    after() throwing(Throwable t) : applicationCode(){
        //    }
            after() returning: applicationCode(){
              Thrower.$throw(new IOException("oh oh"));
          }
            static class Thrower{
              private static Throwable throwable;
                    private Thrower() throws Throwable{
                  throw throwable;
              }
                    public static void $throw(Throwable t){
                  throwable = t;
                  try {
                      Thrower.class.newInstance();
                  } catch (InstantiationException e) {
                      e.printStackTrace();
                  } catch (IllegalAccessException e) {
                      e.printStackTrace();
                  }
              }
                    public static void $throw0(Throwable t){
                  //Questionable but okay for "debugging" purposes
                  Thread.currentThread().stop(t);
              }
          }
        }
        [/code]

        Example:
        [code]
        package de.tutorials.training;

        public class Main {
          public static void main(String[] args) throws Throwable {
              try {
                  m0();
              } catch (Throwable t) {
                  t.printStackTrace();
              }
                    try {
                  m1();
              } catch (Throwable t) {
                  t.printStackTrace();
              }

              try {
                  m2();
              } catch (Throwable t) {
                  t.printStackTrace();
              }
          }

          public static void m0() {}
          public static int m1() { return 0; }
          public static void m2() throws Throwable {}
        }
        [/code]

        Output:
        [code]
        java.io.IOException: oh oh
          at
        de.tutorials.training.aspects.ExampleAspect.ajc$afterReturning$de_tutorials_training_aspects_ExampleAspect$1$d15ea11e(ExampleAspect.aj:12)

          at de.tutorials.training.Main.m0(Main.java:29)
          at de.tutorials.training.Main.main(Main.java:10)
        java.io.IOException: oh oh
          at
        de.tutorials.training.aspects.ExampleAspect.ajc$afterReturning$de_tutorials_training_aspects_ExampleAspect$1$d15ea11e(ExampleAspect.aj:12)

          at de.tutorials.training.Main.m1(Main.java:32)
          at de.tutorials.training.Main.main(Main.java:15)
        java.io.IOException: oh oh
          at
        de.tutorials.training.aspects.ExampleAspect.ajc$afterReturning$de_tutorials_training_aspects_ExampleAspect$1$d15ea11e(ExampleAspect.aj:12)

          at de.tutorials.training.Main.m2(Main.java:37)
          at de.tutorials.training.Main.main(Main.java:20)
        Exception in thread "main" java.io.IOException: oh oh
          at
        de.tutorials.training.aspects.ExampleAspect.ajc$afterReturning$de_tutorials_training_aspects_ExampleAspect$1$d15ea11e(ExampleAspect.aj:12)

          at de.tutorials.training.Main.main(Main.java:25)

        [/code]

        Best regards,
        Thomas

        Oliver Böhm schrieb:

            Hallo,

            for testing purpose I want to throw an Exception after a
            method returns:

               after() returning throws InterruptedException :
            applicationCode() {
                   throw new InterruptedException();
               }

            This works as expected for methods which throws
            InterruptedException. Now I want to make it more general and
            addresses *all* methods which can throw an checked exception:

               after() returning throws Exception : applicationCode() {
                   ...
               }

            But here I got an error message ("can't throw checked
            exception 'java.lang.Exception' at this join point...")
            which is clear because the pointcut "applicationCode()"
            addresses e.g. an InterruptException.

            Has anybody a hint or tip how I can realize it without
            defining an advice for each possible checked Exception?

            regards
            Oliver
            _______________________________________________
            aspectj-users mailing list
            aspectj-users@xxxxxxxxxxx <mailto:aspectj-users@xxxxxxxxxxx>
            https://dev.eclipse.org/mailman/listinfo/aspectj-users


        _______________________________________________
        aspectj-users mailing list
        aspectj-users@xxxxxxxxxxx <mailto:aspectj-users@xxxxxxxxxxx>
        https://dev.eclipse.org/mailman/listinfo/aspectj-users


-- Oliver Böhm
    http://www.javatux.de

    _______________________________________________
    aspectj-users mailing list
    aspectj-users@xxxxxxxxxxx <mailto: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

--
Oliver Böhm
http://www.javatux.de


Back to the top