Which is better, a return code or an exception?
来源:互联网 发布:淘宝网旗袍中长款 编辑:程序博客网 时间:2024/05/22 00:38
When 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. Using exceptions rather than return codes leads to the most robust applications, and an exception based application can also give the cleanest code, since return codes don't have to be checked after every call, but instead, several calls can be grouped together in one MONITOR block, assuming that the same error handling can be done for all errors. If 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. 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 a exception monitor, so the application will crash. I say this is "safe" because it's better to have an application crash due to an unmonitored error than it is to have it complete without crashing but using invalid data. Here is what the calling code looks like for each of those mechanisms, when written by a careful programmer. Or, if the careful programmer wants the exception to percolate to a higher-level exception handler, they don't need the MONITOR: What if the caller doesn't want to handle the error at that point, but instead, they want some higher-level monitor to handle the error. Now, the programmer has to do more work when they call the procedures that use return codes. Or what if the caller of the procedure that returns the exception doesn't want to leave the message in the joblog? Wouldn't it be nice if the caller could choose how the function was written? Here's how to do that. It's similar to the way the system APIs are written. Write the procedure to signal an exception unless an optional errorcode parameter is passed. Here's the sloppy programmer again. No error code parameter is passed, so the application crashes with an exception. Here's the programmer who wants the exception to percolate to a higher level handler. It looks the same as the call made by the sloppy programmer, and that's ok, since it means the procedure is easy to call. Here's the programmer who doesn't want the exception to go to the joblog: When you use this mechanism, you are free to have your procedure return a useful value. A sloppy programmer calls your procedures
.-----------------------------------------------------------. : rc = returncodeProc(); : : Didn't check the return code : '-----------------------------------------------------------'
.-----------------------------------------------------------. : errParmProc(errorCode); : : Didn't check the error code : '-----------------------------------------------------------'
.-----------------------------------------------------------. : exceptionProc(); : : Didn't monitor for the error, but that's ok since : : the application will crash and someone will notice : '-----------------------------------------------------------'
A careful programmer calls your procedures
.-----------------------------------------------------------. : rc = returncodeProc(); : : if rc = FAIL; : : handle the error condition; : : endif; : '-----------------------------------------------------------'
.-----------------------------------------------------------. : errParmProc(errorCode); : : if errorCode = FAIL; : : handle the error condition; : : endif; : '-----------------------------------------------------------'
.-----------------------------------------------------------. : monitor; : : exceptionProc(); : : on-error; : : handle the error condition : : endmon; : '-----------------------------------------------------------'
.-----------------------------------------------------------. : exceptionProc(); : '-----------------------------------------------------------'
Each mechanism causes awkwardness for some callers
.-----------------------------------------------------------. : rc = returncodeProc(); : : if rc = FAIL; : : signal the error condition; : : endif; : '-----------------------------------------------------------'
.-----------------------------------------------------------. : errParmProc(errorCode); : : if errorCode = FAIL; : : signal the error condition; : : endif; : '-----------------------------------------------------------'
.-----------------------------------------------------------. : monitor; : : exceptionProc(); : : on-error; : : remove the message from the joblog; : : handle the error condition; : : endmon; : '-----------------------------------------------------------'
Best of both worlds
.-----------------------------------------------------------. : errParm_Or_ExceptionProc(); : '-----------------------------------------------------------'
.-----------------------------------------------------------. : errParm_Or_ExceptionProc(); : '-----------------------------------------------------------'
.-----------------------------------------------------------. : errParm_Or_ExceptionProc(errorCode); : : if errorCode = FAIL; : : // handle the error condition : : endif; : '-----------------------------------------------------------'
Use the return value for your application
.-----------------------------------------------------------. : addr = get_cust_addr(id); : '-----------------------------------------------------------'
.-----------------------------------------------------------. : addr = get_cust_addr(id : errcode); : : if errcode.status = ID_NOT_FOUND; : : ... : : endif; : '-----------------------------------------------------------'
- Which is better, a return code or an exception?
- Which is better: AMP or SMP?
- Which is Better: Forms Servlet or Socket Mode?
- Which One Is Better: 2.4 GHz or 5 GHz?
- which would be better served with an electric machine. If your needs are small or moderate
- Which one is better for simulating wireless sensor network (WSN)? omnet++ or Ns-2 Ns-3
- Is science a monster or an angel?
- COleDateTime vs. CTime. Which one is better?
- Return 'null' or throw exception
- which is best to use ? List or Dictionary ? For efficiency of code execution.
- 65. An ounce of luck is better than a pound of wisdom. 聪明才智,不如运气
- '<' is an unknown code
- It is hard to make a better type of code arranging.
- [INFO] Exception Thrown When Trying To Deserialize An Object Whose Type Is Defined In An Assembly Which Is Loaded By Assembly.Lo
- How to allow a user to click on TextBlocks which return an integer ID in the click handler
- What is better: @SuppressLint or @TargetApi?
- Question 8: In which of the following scenarios is a Copy Constructor called or invoked?
- Ruby:The provided regular expression is using multiline anchors (^ or $), which may present a securi
- snmp代理程序
- 端午前夕
- webqq2协议分析和qq聊天机器人简单实现
- 彻底解决C#实现DataTable导出EXCEL表格(转载)
- 类模板
- Which is better, a return code or an exception?
- 在linux下使用libunique实现单实例
- How to Copy an Assembly From the GAC to the File System
- 亲和数问题
- 搜索引擎 apache-solr
- YUI or jquery
- 小猫头鹰的故事
- MySQL 当记录不存在时insert,当记录存在时update
- onRetainNonConfigurationInstance()