My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping @Yeyhi.com

Pages










Thursday, May 28, 2020

Avoid Throwing Raw Exception Types rule in Checkstyle

See a sample violation in java code:

com.puppycrawl.tools.checkstyle.checks.AbstractTypeAwareCheck:299 Rule:AvoidThrowingRawExceptionTypes Priority:1 Avoid throwing raw exception types..
com.puppycrawl.tools.checkstyle.checks.ClassResolver:179 Rule:AvoidThrowingRawExceptionTypes Priority:1 Avoid throwing raw exception types..
com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck:270 Rule:AvoidThrowingRawExceptionTypes Priority:1 Avoid throwing raw exception types..
com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck:321 Rule:AvoidThrowingRawExceptionTypes Priority:1 Avoid throwing raw exception types..
com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck:685 Rule:AvoidThrowingRawExceptionTypes Priority:1 Avoid throwing raw exception types..
com.puppycrawl.tools.checkstyle.checks.indentation.HandlerFactory:68 Rule:AvoidThrowingRawExceptionTypes Priority:1 Avoid throwing raw exception types..
com.puppycrawl.tools.checkstyle.checks.indentation.HandlerFactory:169 Rule:AvoidThrowingRawExceptionTypes Priority:1 Avoid throwing raw exception types..
com.puppycrawl.tools.checkstyle.checks.indentation.HandlerFactory:174 Rule:AvoidThrowingRawExceptionTypes Priority:1 Avoid throwing raw exception types..
com.puppycrawl.tools.checkstyle.checks.indentation.HandlerFactory:177 Rule:AvoidThrowingRawExceptionTypes Priority:1 Avoid throwing raw exception types..


So there comes a question what do we want to use instead of RuntimeException? Is private exception with descriptive name what went wrong good approach for you? Or we should create package with exceptions to choose from? I'll try to use standard JDK exceptions where possible.
It's also possible to create RuntimeCheckstyleException, but I don't think it is good idea.

To be specific we might also want to know what do we want to use instead of RuntimeException?
That is very controversial question. As demand for more specific type of Exception is clear , especially when we have a lot of exception types in our disposal (http://docs.oracle.com/javase/7/docs/api/java/lang/RuntimeException.html?is-external=true) even in standard JDK.

But some time where is no need to be specific and exception is signal to stop processing. Details in message is good enough - it is very good practice to keep message informative, exception type will allow only in creation additional logic but that is contradict with "ExceptionAsFlowControl".


We need to work on these validations if it comes. Consider a little redesign of your code.

No comments:

Post a Comment