I heard people say exception handling is a bit expensive because of the stack unwinding.
I don't get something, the stack unwinding happens whether I throw an exception and whether I use "return". So where is the difference?
If for example I get a memory problem that I can't handle - the only option is to stop the function till the I reach the area where the problem should be handled or notified. So what's my other option for throwing an exception?
I can use "return" instead of throwing exception, but then it's the same. I know stack unwinding can go even six stacks back, but so checking return value and "return" combined.
An explanation will be welcomed.
When you use a return, the stack is "unwound" unconditionally, which can conceptually be as simple as executing a single "ret" machine code instruction. Under exceptions, the stack unwinding has to search for a suitable exception handler, which is a far more complex task. The exception path also has the task of constructing and probably copying the exception object, which may not be trivial.