I know you’re asking about performance overhead, but you really should not use try
/catch
and if
interchangeably.
try
/catch
is for things that go wrong that are outside of your control and not in the normal program flow. For example, trying to write to a file and the file system is full? That situation should typically be handled with try
/catch
.
if
statements should be normal flow and ordinary error checking. So, for example, user fails to populate a required input field? Use if
for that, not try
/catch
.
It seems to me that your example code strongly suggests that the correct approach there is an if
statement and not a try
/catch
.
To answer your question, I would surmise that there is generally more overhead in a try
/catch
than an if
. To know for sure, get a Java profiler and find out for the specific code you care about. It’s possible that the answer may vary depending on the situation.