Performance cost of coding “exception driven development” in Java?

前端 未结 14 1665
生来不讨喜
生来不讨喜 2020-12-09 10:28

Are there are any performance cost by creating, throwing and catching exceptions in Java?

I am planing to add \'exception driven development\' into a larger project.

14条回答
  •  天命终不由人
    2020-12-09 11:04

    Personally, I think that's a bad and obnoxious idea.

    The usual way to encourage people to check for null values is to use annotations like @Nullable (and its opposite, @NotNull, for functions that are guaranteed to return non-nulls). By setting up similar annotations on parameters (so that parameter expectations are set), quality IDEs and bug-checkers (like FindBugs) can then generate all the necessary warnings when the code doesn't do enough checking.

    Those annotations are available in JSR-305, and apparently there is also a reference implementation.


    As far as performance goes, creating the exception is the expensive part (I've read that it's due to the stacktrace-filling, among other things). Throwing the exception is cheap, and is a control-transfer technique used in JRuby.

提交回复
热议问题