什么时候需要使用try-catch
代码执行预料不到的情况,或出错的可能性很大时,使用try-catch语句
构造一个文件输入流(上传文件时,线上环境的内存情况不确定)出错的可能性很大
文件上传写入, 数据库事务的提交,还有摄像头和打印机的使用
使用数据库事务的时候使用try-catch,如果事务执行成功就提交事务,如果事务执行失败就由catch提示错误并回滚事务。还有就是在使用curl方式访问其他网络地址的时候会用到,如果网络访问出错或者网络访问超时就在catch中抛出错误。还有就是之前写winfrom软件的时候调用摄像头和打印机,会使用try-catch。
程序调用其他人写的程序接口的时候,不敢保证别人的接口返回的都是约定好的返回值。所以如果接口返回约定好的返回值,那么try中的程序正常执行,如果意料之外catch抛出错误。
Return codes are more verbose
if(doSomething())
{
if(doSomethingElse())
{
if(doSomethingElseAgain())
{
// etc.
}
else
{
// react to failure of doSomethingElseAgain
}
}
else
{
// react to failure of doSomethingElse
}
}
else
{
// react to failure of doSomething
}
This code could well be translated into:
try
{
doSomething() ;
doSomethingElse() ;
doSomethingElseAgain() ;
}
catch(const SomethingException & e)
{
// react to failure of doSomething
}
catch(const SomethingElseException & e)
{
// react to failure of doSomethingElse
}
catch(const SomethingElseAgainException & e)
{
// react to failure of doSomethingElseAgain
}
With the above examples, assume than someone forgets to handle its possible error. The error is ignored when "returned", and will possibly explode later. The same problem won't happen with exception
Return codes are not a universal solution
Return Codes means you can't chain expressions
Exception are typed
You can send different classes for each kind of exception.
Don't ever use catch(...) without rethrowing
The solution could be mixing of return code and exceptions
The solution is to throw when something should not happen. And when something can happen, then use a return code or a parameter to enable to user to react to it
So, the only question is "what is something that should not happen?"
It depends on the contract of your function
Conclusion
When you code using return codes, you're preparing yourself for failure, and hope your fortress of tests is secure enough
When you code using exception, you know that your code can fail, and usually put counterfire catch at chosen strategic position in your code. But usually, your code is more about "what it must do" then "what I fear will happen"
But when you code at all, you must use the best tool at your disposal, and sometimes, it is "Never hide an error, and show it as soon as possible".
什么时候需要使用try-catch的更多相关文章
- SQLServer如何添加try catch
在.net中我们经常用到try catch.不过在sqlserver中我们也可以使用try catch捕捉错误,在这里把语法记录下来和大家分享一下, --构建存储过程CREATE PROCEDURE ...
- try...catch..finally
try..catch..finally try{ 代码块1 }catch(Exception e){ 代码块2 }finally{ 代码块3 } catch是抓取代码块1中的异常 代码块2是出异常后的 ...
- C++异常处理:try,catch,throw,finally的用法
写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- [c#基础]关于try...catch最常见的笔试题
引言 在翻看之前总结的常见面试题中,关于try...catch异常处理的还是蛮多了,今天看到这个面试题,也就重新学习一下. try..catch语法 try-catch语句由一个try块后跟一个或多个 ...
- 高程(4):执行环境、作用域、上下文执行过程、垃圾收集、try...catch...
高程三 4.2.4.3 一.执行环境 1.全局执行环境是最外层的执行环境. 2.每个函数都有自己的执行环境,执行函数时,函数环境就会被推入一个当前环境栈中,执行完毕,栈将其环境弹出,把控制器返回给之前 ...
- try catch里面try catch嵌套
try catch里能否内嵌try catch?答案是肯定的.但是等内层try catch出异常之后是个什么执行顺序呢?看下面代码 static void Main(string[] args) { ...
- 基础知识《十》java 异常捕捉 ( try catch finally ) 你真的掌握了吗?
本文转载自 java 异常捕捉 ( try catch finally ) 你真的掌握了吗? 前言:java 中的异常处理机制你真的理解了吗?掌握了吗?catch 体里遇到 return 是怎么处理 ...
- java try(){}catch(){}自动资源释放
从 Java 7 build 105 版本开始,Java 7 的编译器和运行环境支持新的 try-with-resources 语句,称为 ARM 块(Automatic Resource Manag ...
- Java throws Exception、try、catch
throws Exception是方法后面接的 意思是向上级抛出异常 try{}里面的异常会被外面的catch捕捉到 抛出异常是throw new Exception("异常"); ...
随机推荐
- 自动化测试工具selenium的常用定位方法
定位方法不仅限于这些,我也会随时补充,大家有其他补充或建议可以在评论区一起讨论哦!!! [打开链接]drive.get("https://www.baidu.com") ...
- Loadrunner——调试及脚本编译
调试一般用于运行代码是出现的错误. loadrunner调试方式:断点.单步跟踪.日志输出.值查看器等, 断点设置 断点插入的位置:非空行或非语句的起始,简单来说呢就是断点打在函数前(取消断点就直接在 ...
- python笔记:list--pop与remove的区别
正常情况下: # coding=utf-8 fruit = ['apple', 'pear', 'banana' ] #指定索引删除 fruit.pop(0) #符合元素删除,具体数值 fruit.r ...
- 关于vue模版动态加载按照指定条件
一.在data中定义要作为模版的变量,当前定义了两个 menuNavigation 和menuDetails 二.模版使用方式使用component中的 用v-bind:is 来使用其参数
- k8s集群查看node和pod的实时资源使用情况
一.部署步骤 1.准备metrics-server官方yaml文件 2.部署metrics-server 3.查看资源使用情况 二.准备metrics-server官方yaml文件 [root@loc ...
- goland 快捷键
goland常用快捷键 Coldestmonth 2018-07-17 17:26:37 18067 收藏 14版权Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/-/ )Ctrl+D ...
- 如何跳出forEach循环
for(let ii in this.listData){ console.log("提交前数据",ii) try{ this.listData[ii].forEach((el,i ...
- applicationContext使用注解代替
创建一个类SpringConfig @Configuration//证明这个类是spring的配置文件类 @ComponentScan("com.itheima")//扫描的哪些包 ...
- Q:Win10无法访问共享文件夹。提示此用户无法登录,因为该账户当前已被禁用
问题:当我访问同事电脑共享文件夹时,弹出如下提示框: 可以在命令提示符上ping通对方主机,但是不能访问对方文件夹 尝试解决方法(无效): 1.同时按住win+r打开运行命令框,输入gpedit.ms ...
- 泛型 ? extends E ? super E的区别
package cn.itcast.generic; import java.util.ArrayList;import java.util.Collection; public class Supp ...