I usually use exceptions as a last resort. If this is something that should never happen, and the language supports it, I will use some kind of statement (since this happens, of course, it is some mistake).
If this can be avoided, I will prefer the error code.
Of course, there are some cases in some languages ββwhere you have little choice. For example, C ++ constructors cannot have any return value, and it is probably bad to have a partially constructed object, so it is sometimes preferable to throw an exception.
In general, if you can test the error condition and just report the error to the caller in a way other than the exception, I would prefer this.
source share