Catch Throw Catch

2003-10-04
2012-10-07
  • Will Sargent

    Will Sargent - 2003-10-04

    I've never seen this one before, but I think that as a rule, a catch should never throw again unless it is passing it out of the method (as in "throws Exception").

    try {
       ...
       try {
          ...
       } catch (Exception e) {
           // do stuff
           throw e;
       }
    } catch (Exception e) {
       // do more stuff
      
    }

     
    • Tom Copeland

      Tom Copeland - 2003-10-06

      Yup, using exceptions as flow control... as a goto, almost.   Blah.

      Tom

       
    • David Dixon-Peugh

      Never seen this before, but here is something nasty.
      (Explicit use of Exception as flow-control.)

      public void foo() {
        try {
           for (int i = 0; i < 100; i++) {
              if (i == 98) throw new Exception();
           }
         } catch (Exception e) {}
      }

      Stupid example, but I think you can see the point.
      Rather than putting the condition in the loop, or
      using the "break", and exception is thrown to stop
      the loop.

      I suppose what we could do to catch this, is look
      for all Try blocks which *explicitely* throws the same
      exception that is being caught.  (If its thrown by
      a method, thats OK.)

      Thoughts?

       

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks