Sunday, March 30, 2008

Exception handling versus error codes

A while ago, I was really lost between throwing/catching exceptions and returning error codes. After doing some research I have reached the following:
Exception handling is usually expensive due to the stack unwinding process, specially when it is not caught near where it was thrown.

So you can't just go throwing an exception for every trivial error or an invalid input to a procedure. This leads me to believe that a good approach 'd be to check all the inputs given to a method using normal if/else blocks,returning failure status or error codes when something goes wrong or wrong input is provided and only throwing an exception when a fatal error occurs.

No comments: