0

I'm trying to identify cases where throw statements occur inside catch blocks. For instance:

catch(MyException e){
   throw e;
}
catch(Exception e){
    throw new AnotherType();
}

Is it possible to access the explicit begin/end range of each catch block to iterate over its units in order to check for the presence of a throw statement? Any idea on how I can do this?

EijiAdachi
  • 441
  • 1
  • 3
  • 15
  • 1
    Why do need to do this? – Kevin Krumwiede Jul 15 '14 at 17:56
  • developing tool to check some exception handling contracts – EijiAdachi Jul 15 '14 at 17:57
  • The compiler already does that. What functionality are you trying to add? – Kevin Krumwiede Jul 15 '14 at 18:01
  • I'm just trying to check if a given method rethrows the caught exception, or if it remaps it to another type. This is related to some design rules enforcement I'm trying to implement. – EijiAdachi Jul 15 '14 at 18:09
  • So this is some kind of static code analysis? If so, then I agree with Dan's answer. Code that remaps an exception should record the original exception as the new exception's cause. It shouldn't be hard to look for occurrences of "throw new ..." in a catch block and ensure that they use a constructor that takes the original exception. – Kevin Krumwiede Jul 15 '14 at 19:25
  • "I have use SOOT". Why? – Ira Baxter Jul 16 '14 at 02:09
  • Because its part of a larger research project and it has to be integrated to it – EijiAdachi Jul 16 '14 at 13:57
  • Your research project probably uses a GUI, too, but you aren't likely to use that in detecting nested throws. Back to, "why Soot?" You won't get a lot of sympathy at SO by saying, "I don't know how to do X" without showing us what you have (e.g., explain what Soot is abstractly) and why it can or cannot provide the data you want, and why you cannot extract it. – Ira Baxter Jul 20 '14 at 11:36
  • Soot is a framework for analyzing and transforming Java bytecode. It provides 4 intermediate representations of bytecode. Since in byte code the end of a catch block is not explicitly marked, I was wondering if Soot has any type of functionality to detect the end of a catch block; as far as I know, it only provides a pointer to the begin of a catch block. – EijiAdachi Jul 21 '14 at 03:37
  • OK, you've told us the information you need isn't in Soot, so it doesn't matter whether you use it: it won't provide what you need. So you need some other program representation where you can get this data. As a general rule, you will likely need the AST to determine the bounds of each catch block, and which blocks nest inside it. – Ira Baxter Jul 23 '14 at 03:17

2 Answers2

1

Looking at your tags I assume you are using Soot.

You can use the traps of the JimpleBody you are looking at to determine whether a statement is in a try/catch block. Just call body.getTraps().

Eric
  • 1,343
  • 1
  • 11
  • 19
0

If you just want to do this as static code analysis I would suggest you use a regular expression in a find operation of an IDE to locate any instances of a throw within the catch parenthesis. Certainly supported in IntelliJ.

Dan
  • 9,681
  • 14
  • 55
  • 70
  • I have to use SOOT to do that. – EijiAdachi Jul 15 '14 at 20:49
  • 1
    As a general rule, using regular expressions to navigate through nested structures just doesn't work: an intuition is that regexes in effect cannot count, and to navigate nested structures, you have to count nesting. This link seems to be about HTML, but it is really about nested text structures and why people seem to fail to understand the futility of using regexes for this: http://stackoverflow.com/a/1732454/120163 – Ira Baxter Jul 16 '14 at 02:08