Skip to content

new Check: ForbidThrowAnonymousExceptions #117

Closed
romani opened this Issue Jun 27, 2013 · 3 comments

4 participants

@romani
open source initiative member
romani commented Jun 27, 2013

Description and discussion of the problem: http://stackoverflow.com/questions/17334498/throw-anonymous-exceptions-in-java

Task: create new Check that catch that situation, add option to create list of ignored Exceptions to instantiate (there could be a lot of legacy code, and some time it could be very useful - but it is more exception than rule).

Forbid throwing anonymous exception. limitation: This Check does not validate cases then Exception object is created before it is thrown.

For example:

catch (Exception e) {
   throw new RuntimeException() { //anonymous exception
    //some code
   };
@miraxes miraxes was assigned Jul 17, 2013
@isopov
open source initiative member
isopov commented Jul 23, 2013

With Checkstyle it is not possible to check the case when anonymous exception is created in one source file and thrown in the other. However during exception creation it is also not possible to know whether this anonymous class is exception or not. So this check inside Checkstyle will miss some cases.

I think it is another idea that is worth adding to some other tooling capable of operating on class-files and reading the whole inheritance chain.

@romani
open source initiative member
romani commented Jul 24, 2013

It is clear that checkstyle will not be able cover all cases - and will not be part of main Checkstyle project.
One more case: anonymous exception is created by numerous calls of methods to create it.
Exception class detection could be done if creation is same catch block by detecting "Exception" at the end of class name - this will cover 95% of cases.

@maxvetrenko maxvetrenko was assigned by romani Mar 26, 2014
@maxvetrenko maxvetrenko added a commit to maxvetrenko/sevntu.checkstyle that referenced this issue Apr 10, 2014
@maxvetrenko maxvetrenko Fixed #117. ForbidThrowAnonymousExceptions was introduced. 4162f8d
@maxvetrenko maxvetrenko added a commit to maxvetrenko/sevntu.checkstyle that referenced this issue Apr 10, 2014
@maxvetrenko maxvetrenko Fixed #117. ForbidThrowAnonymousExceptions was introduced. 7fc13fe
@romani romani closed this in 4ea10c1 Apr 10, 2014
@romani
open source initiative member
romani commented Apr 10, 2014

Current Check implementation limitation will be resolved at #183

@romani romani reopened this Apr 10, 2014
@romani romani closed this Apr 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.