Java – is try / catch a resource consuming operation?

I've heard that it's more reasonable to return null in a method instead of throwing exceptions in a try / catch block and catching them, rather than because try / catch is a resource consuming operation It's true?

Solution

What you hear is true: throwing and catching exceptions is more expensive than returning null However, please consider the cost of all callers maintaining the code They must check for null, and if you want to add more kinds of errors, you will have to modify your caller

Use exceptions for exceptions If an error is common or common, then this is no exception: a status code is returned

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>