Please don’t lets cargo-cult the idea that exceptions are /always/ bad. As with any other technique, if performance is critical you should benchmark.
For language exceptions the performance costs break down into two broad categories:
-
The cost of setting things up so it’s possible to throw an exception
-
The cost of actually throwing an exception.
The first cost may be unavoidable - it depends on the language. For example some C++ implementations generate slightly faster function entry code if the compiler knows that it won’t subsequently have to unwind the stack due to an exception. Higher level languages tend to incur the ‘being able to handle an exception’ cost whether you actually throw an exception or not - and in general that cost will be small compared to whatever else the language is doing.
Actually throwing an exception will also take some time - how much depends on the implementation.
Measure then decide. Don’t cargo cult.