3 Replies Latest reply on Oct 28, 2009 4:59 AM by Andrew Dinn

    Throw undeclared checked exception inside try block?

    Chris Bredesen Newbie

      I'm just starting to use Byteman to drive toward 100% test coverage on my project. Well done Andrew & team, this is a great help!

      I've come across what I think is a limitation but I'm not sure if it's a JVM limitation or an unimplemented feature.

      I've got a method that looks something like this:

      public void foo() { // no throws here
       try {
       bar(); // throws checked MyException
       } catch (MyException e) {
       // wrap and throw my own app exception
       }
      }


      This call to bar() is an EJB invocation over which I have no control. It throws a checked Exeption which is difficult to trigger. I've mocked the EJB but I'd like to use Byteman to generate the exceptional case.

      When I try this, I get:

      Rule.ensureTypeCheckedCompiled : error type checking rule throw <Exception> in <method name>
      org.jboss.byteman.rule.exception.TypeException: ThrowExpression.typeCheck :
       exception type not declared by trigger method


      It's logically prohibited to generate a checked exception within a method that does not declare it, but in this case I'm inside a try block which specifically handles the checked Exception. It is this code that I'm attempting to cover.

      Am I going about this wrongly?

      Cheers,

      Chris

        • 1. Re: Throw undeclared checked exception inside try block?
          Andrew Dinn Master


          I'm just starting to use Byteman to drive toward 100% test coverage on my project. Well done Andrew & team, this is a great help!



          Team? Wanna join?


          Am I going about this wrongly?


          Well, assuming that your rule is like this:

          RULE blah de blah
          CLASS Foo
          METHOD foo
          AT INVOKE BAR
          IF TRUE
          DO THROW MyException()
          


          then I am afraid so.

          The problem is to do with what THROW means. The exception is thrown from the <em>trigger method</em> not the trigger point. THROW implies a short circuit of the whole method and throw an exception to the caller of foo(), just as RETURN means return to the caller of foo().

          What you need to do is inject the THROW into bar() as follows:

          RULE blah de bar
          CLASS Foo
          METHOD bar
          AT ENTRY
          IF TRUE
          DO THROW MyException()
          


          The only problem is that this clobbers <em>all</em> calls to bar(). So, how to get round this? Well, use a counter or flag or something to define the requisite logic in the condition of the rule on bar e.g.

          RULE blah de bar
          CLASS Foo
          METHOD bar
          AT ENTRY
          IF flagged($0)
          DO THROW MyException()
          
          RULE blah de foo
          CLASS Foo
          METHOD foo
          AT INVOKE bar
          IF TRUE
          DO flag($0)
          
          RULE blah de foo de doo
          CLASS Foo
          METHOD foo
          AT EXIT
          IF TRUE
          DO cl;ear($0)
          


          The second and third rules, respectively, set and clear a flag value identified using $0, the target object for the call to foo. This makes sure the call to bar() only barfs when it is made inside a call to foo with the same recipient.


          • 2. Re: Throw undeclared checked exception inside try block?
            Chris Bredesen Newbie

             

            The exception is thrown from the trigger method not the trigger point.


            Makes perfect sense now that I've re-read the doco. Cheers!

            • 3. Re: Throw undeclared checked exception inside try block?
              Andrew Dinn Master

               


              Makes perfect sense now that I've re-read the doco. Cheers!


              When all else fails . . .