3

I would rather not add/remove throws clauses manually, especially while prototyping. Is there a batch refactoring tool that adds/removes throws clauses to every method to reflect the code? After all, the compiler tells you what exceptions a method may throw, therefore maybe a tool exists that uses that information to manipulate throws clauses.

EDIT: Before I clarified that I am looking for a batch tool, somebody has mentioned a manual intervention via IDE, but this is not what I am looking for. I am looking for a batch utility.

Eleno
  • 2,864
  • 3
  • 33
  • 39
  • 1
    You need them just as much then as any other time. Saves you writing unimplementable crap, for one thing. – user207421 Mar 02 '16 at 09:58

2 Answers2

1

ProjectLombok has a wonderful feature called SneakyThrows (It may fit your use case in prototyping.)

From their website you can do the following without a compile error :

@SneakyThrows(UnsupportedEncodingException.class)
public String utf8ToString(byte[] bytes) {
  return new String(bytes, "UTF-8");
}

@SneakyThrows
public void run() {
  throw new Throwable();
}
rjdkolb
  • 10,377
  • 11
  • 69
  • 89
0

I suggest you use your IDE to add checked exceptions to your method unless you intend to handle that exception. This way you code won't be a mess when it comes to production-ising it. i.e. you don't have to think about it, just let your IDE do the work.

In IntelliJ, I select the line which has a checked exception and hit <Alt> + <Enter> as I do to fix most errors and select Add exception to method signature which can also add it to any method overridden as well.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • > just let your IDE do the work. This does not seem the case, as I must click on every line. I am looking for an automated tool. – Eleno Mar 02 '16 at 10:04
  • 1
    @Elena how many lines are we talking about? there shouldn't be more than dozens. – Peter Lawrey Mar 02 '16 at 10:07