Yet another inspection: Exception created but not thrown

Found this in my code:

...
new SorryException("Can't do that, Dave");
...

It's caught by the inspection "Result of object allocation ignored" but I
do think that a more strict inspection could be done here. Suggested
name is "Exception created but not thrown".

/Mikael


1 comment
Comment actions Permalink

Trivial to do, but I know there are some not-too-common-but-not-stupid-either coding patterns that would show up as false positives. For instance


throw buildMeABusinessException(bunch, of, args);

...

public Exception buildMeABusinessException(Object... args)
{
//complex code that creates specific exceptions and returns them, thus triggering the inspection
}


Less commonly, there are also people who commonly do this

Exception x = new MyExceptionClass(x);
throw x;

so as to give them a place to put a breakpoint between the exception creation and throw. Special-casing to avoid this as a false positive would be pretty easy, though.

Certainly worth opening a JIRA for, though.

--Dave Griffith

0

Please sign in to leave a comment.