You are in: RPG Cafe > Return code vs exception?
Short URL: https://ibm.biz/rpgcafe_return_code_vs_exception
RPG Cafe: Return code or exception?
Which is better, a return code or an exception?
When you are writing a procedure that might want to indicate an error condition, you have three choices: You can use the return value to indicate success or failure, you can have a separate “return code” parameter, or you can send an exception to indicate failure.
An application that uses exceptions is more robust than an application that uses return codes. An application that uses exceptions can also give the cleanest code, since return codes don’t have to be checked after every call. Instead, several calls can be coded in one MONITOR block, assuming that the same error handling can be done for all errors.
When you write a procedure that requires the caller to add code to check a return code, then the application is weakened every time the caller fails to check the return code.
A sloppy programmer calls your procedures
Here is what the calling code looks like for each of those three mechanisms, when written by a sloppy programmer. The only one that is “safe” is the one where the procedure returns an exception. The caller didn’t code an exception monitor, so the application c