什么时候需要使用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("异常"); ...
随机推荐
- c++学习 4 运算符及其应用技巧
一 按位与运算符 "&"按位与运算符,全1为1,有0则0. 特点:和1相与保持不变,和0相与都变为0. 功能:将指定位置清0. example: 1000 1100 &am ...
- IP协议数据包
Header Length:头部长度固定20字节,永远为5(4bit为单位) Total Length:头部+包, 抓包结果 Identification.Fragment Flags.Fragmen ...
- springboot pom文件引入本地jar包
记录maven引用本地jar包 配置 及打包的其中一个方法,作为个人笔记,供参考: <dependency> <groupId>gdin</groupId> < ...
- 解决spring boot 2.6.4版本循环依赖问题
引用spring boot 2.6.4 版本 报循环依赖错误 *************************** APPLICATION FAILED TO START ************* ...
- Linux(2)
虚拟机关键配置名词解释 远程链接工具 linux准则 远程链接工具快捷键 系统相关命令 文件相关命令 linux目录结构 虚拟机关键配置名词解释 # 虚拟网络编辑器说明 桥接模式 # 可以访问互联网 ...
- IM系统功能简版图(v0.1)持续更新
- 磊磊零基础打卡算法:day19 c++字符串hash
5.22 字符串hash: 字符哈希串的意思 其实就是将字符串的前缀转换为数来存值由于每位的权值是不一样的 所以每个前缀值都对应着唯一的一种字符串: 主要用途:字符串/数据的比较,是kmp的一种替代: ...
- Ubuntu VMWare安装纪要
一.VMware虚拟机下载与安装 版本:VMware Workstation 16 Pro 二.Ubuntu下载与安装 版本:ubuntu-20.04.2.0-desktop-amd64.iso 三. ...
- 微信小程序按下去的样式
微信小程序设置 hover-class,实现点击态效果 目前支持 hover-class 属性的组件有三个:view.button.navigator. 不支持 hover-class 属性的组件,同 ...
- Oracle Fusion Middleware Introduction
Oracle Fusion Middleware Oracle Fusion Middleware is a comprehensive family of software products tha ...