During a discussion and some research regarding using exceptions vs returning values, the following caught my mind:
It is cheaper and provides better performance to return a value, whereas throwing an exception requires the creation of a new object and stack unwinding.
Now, I read up on stack unwinding and from my understanding it is the abrupt process of releasing all allocated resources on the current stack frame before jumping back to the next frame on the stack. In Java specifically, no resource is released but instead all objects currently in scope get dereferenced and become eligible for GC. In the case of a graceful return on the other hand, the objects are immediately destroyed due to going out of scope.
The question is this:
Having a method that has declared a small amount of instances (e.g. 3) and assuming that if an exception is thrown, it will be caught on the next level (it won't climb 5 stack levels for example), is there really a noticeable (not sure if this is the correct word to use) difference compared to returning a value, performance wise? I know that at the end of the day in a simple scenario there is not going to be a noticeable difference but could it lead to one in a bigger system?
Please assume that it makes sense to throw an exception in that point in the method (it is an exceptional situation) and it is not for control flow (which is a known mistake). Returning a value would be preferred only for performance reasons.