Automatically adding/removing checked exceptions while prototyping

108 Views Asked by At

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.

2

There are 2 best solutions below

2
Peter Lawrey On

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.

0
rjdkolb On

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();
}