#34 should check for empty exception handler

release_2.3
closed
None
6
2012-10-10
2002-02-11
Anonymous
No

A lot of Java programmers have a tendency to suppress
the Checked Exception in Java using the
<pre>
catch(Exception e) {
}

</pre>

coding style! This is very irritating. Can you please
make checkstyle detect this by looking for atleast one
non-commented line of source code.

Discussion

  • Oliver Burn

    Oliver Burn - 2002-02-13

    Logged In: YES
    user_id=218824

    Nice idea - should be easy to check that there are some
    characters between the {}'s.

     
  • Vivek Venugopalan

    Logged In: YES
    user_id=131737

    Actually if you can look for non-commented source code
    lines other than something like e=e that would be great.
    Now does your reply mean that we can see it in a future
    version soon :-)?

    I am the original poster of the request. Forgot to log on

     
  • Jens Krinke

    Jens Krinke - 2002-02-14

    Logged In: YES
    user_id=345110

    There are two comments:
    1. Is there any adequat circumstance for emptiness between
    {}? I belive not, so a more general check for {} is probably
    better.

    1. Programmers should never catch exceptions of class
      "Exception".
     
  • Lars Kühne

    Lars Kühne - 2002-02-15

    Logged In: YES
    user_id=401384

    The requirement to ignore statements that have no effect is
    impossible to implement - it is undecidable if a statement
    has any effect. If we ignore "e=e" the developer can write
    "e=(e+1)-1" or "doNothing()".

    I'm not sure that you should never catch "Exception".
    Sometimes you don't have a choice, e.g. if you use badly
    designed third party libraries. I guess the rule is that you
    should not throw an Exception?

     
  • Jens Krinke

    Jens Krinke - 2002-02-26

    Logged In: YES
    user_id=345110

    The rule is to never catch an exception of class
    "Exception" --it's too general. In case of bad libraries,
    you can disable the check. There are many other exceptions
    you shouldn't catch: NullPointer, IndexOutOfBounds etc.

     
  • Vivek Venugopalan

    Logged In: YES
    user_id=131737

    I think we are convoluting my original request. My request
    was to stop people from writing code such as catch(...){}.
    The empty braces were important. Now we are talking about
    catching Exception vs. derived classes of exception and
    catching runtime exception. I think that should be a
    separate enhancement request.

     
  • Lars Kühne

    Lars Kühne - 2002-05-15

    Logged In: YES
    user_id=401384

    This is now implemented in CVS and will be included in the
    next release.

    The implementation only considers statements inside the
    catch block, i.e. comments are ignored. This means that code
    like

    catch (Exception ex) {
    // This cannot happen
    }

    is also flagged as an error, see the docs/engine.html for a
    workaround if you really need to ignore this individual
    exception. Hope this behaviour is OK.

     
  • Vivek Venugopalan

    Logged In: YES
    user_id=131737

    Great Thanks.

    Vivk

     
  • Oliver Burn

    Oliver Burn - 2002-05-26

    Logged In: YES
    user_id=218824

    Updated the check to support:

    • ignore
    • require text (default)
    • require statments
     

Log in to post a comment.