Throwing an exception is extremely expensive compared to checking the value of an integer. However, this does not matter. More importantly, even if the exceptions were cheap, they would still be the wrong choice. The exception is that it is an exceptional event. Exceptions should ideally be used only to represent something unexpected, rare, and preferably fatal.
Another way to look at this: if you are accessing an array beyond its borders, you have an error. Correct the mistake. The exception handler hides the error; it does not fix the error.
source share