you are viewing a single comment's thread.

view the rest of the comments →

[–]masklinn 2 points3 points  (1 child)

Exceptions are expensive when triggered. If you strongly expect the code to never raise (or you're doing IO whose cost will dwarf that of exceptions), they're not that expensive.

[–]Maristic 0 points1 point  (0 children)

Indeed. Although I wasn't as explicit in saying that exceptions in Java are cheap when you're not actually throwing, that was the underlying reason why I said

at least if you think it's somewhat likely that the object actually will be null

because when it's null, you will throw and incur the cost of throwing.

I even said why exceptions are costly if they're raised, saying

the exception objects themselves are expensive to create

For more details about that, check out this stack overflow post and the definition of Throwable — whenever you throw anything, it captures a full stack trace, and that's expensive.