Search code examples
javacatch-blockmulti-catch

Automaticall replacing single catch clauses with duplicate catch clauses


Does anyone know whether there is a tool or something similar to replace single catch clauses with duplicate code within it in java with multiple catch clauses to remove duplicate code?

Context is an upgrade vom java jdk6 to 7.

Thanks and cheers, Ste


Solution

  • I don't think there is a automatic way, You can do something like following

        try {
           // code
        } catch (Exception1 e1) {
          // caught Exception1
        } catch (Exception2 e2) {
          // caught Exception2
        }
    

    You can change this to

        try {
           // code
        } catch (Exception1|Exception2 e) {
          // caught Exception1 or Exception2
        }