I heard that instead of throwing exceptions and catching them in try/catch block it is more reasonable to return null in methods instead because try/catch is resources consuming operation. Is this true?
I heard that instead of throwing exceptions and catching them in try/catch block it
Share
It is true what you have heard: a thrown and caught exception is more expensive than returning
null. But, consider also the expense of maintaining all the callers of your code. They will have to check fornull, and should you ever want to add more kinds of errors you will have to modify your callers.Use exceptions for exceptional conditions. If an error is a common or usual case, then it’s not exceptional: return a status code.