Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6626834

Exception handling : catch multiple exceptions in single catch clause

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 7
    • specification
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      A code snippet as following has few problems.'
       catch (IllegalArgumentException e ) {
      e.printStackTrace();
      } catch (IllegalAccessException e) {
      e.printStackTrace();
      } catch (SecurityException e) {
      e.printStackTrace();
      } catch (NoSuchMethodException e) {
      e.printStackTrace();
      } catch (InvocationTargetException e) {
      e.printStackTrace();
      } catch (InstantiationException e) {
      e.printStackTrace();
      }
      Though for all of the catch clauses it does the same operation, we have to write mutiple catch clauses. Instead would it not be better to handle like the following :
      catch (IllegalArgumentException e || IllegalAccessException e1 ||(SecurityException e2 || NoSuchMethodException e3 || InvocationTargetException e4 || InstantiationException e5) {
      // do something
      }

      JUSTIFICATION :
      - Removes extra code
      - Becomes modular

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      catch (IllegalArgumentException e || IllegalAccessException e1 ||(SecurityException e2 || NoSuchMethodException e3 || InvocationTargetException e4 || InstantiationException e5) {
      // do something
      }
      ACTUAL -
      catch (IllegalArgumentException e ) {
      e.printStackTrace();
      } catch (IllegalAccessException e) {
      e.printStackTrace();
      } catch (SecurityException e) {
      e.printStackTrace();
      } catch (NoSuchMethodException e) {
      e.printStackTrace();
      } catch (InvocationTargetException e) {
      e.printStackTrace();
      } catch (InstantiationException e) {
      e.printStackTrace();
      }

      ---------- BEGIN SOURCE ----------
      catch (IllegalArgumentException e ) {
      e.printStackTrace();
      } catch (IllegalAccessException e) {
      e.printStackTrace();
      } catch (SecurityException e) {
      e.printStackTrace();
      } catch (NoSuchMethodException e) {
      e.printStackTrace();
      } catch (InvocationTargetException e) {
      e.printStackTrace();
      } catch (InstantiationException e) {
      e.printStackTrace();
      }


      to be :

      catch (IllegalArgumentException e || IllegalAccessException e1 ||(SecurityException e2 || NoSuchMethodException e3 || InvocationTargetException e4 || InstantiationException e5) {
      // do something
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      yes. There are workarounds.

            abuckley Alex Buckley
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: