什么时候需要使用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") ...
- FPGA串口 波特率的计数器值
开发板时钟为50Mhz, t为 20ns; xxx波特率时指每秒传xxx bit字节数据.也就是T=1/xxx; 再用T/t就可以得出波特率的计数周期了: 例如9600:T=1/96000=1.041 ...
- create-react-app react 使用dll抽离公共库,大幅缩减项目体积,及项目打包速度
1.安装依赖(clean-webpack-plugin.add-asset-html-webpack-plugin.webpack-cli) yarn add clean-webpack-plugin ...
- redis 配置哨兵模式时出现的问题(redis 版本 6.2.5)
今天准备搭建一个 redis 集群(redis 版本 6.2.5),在这之前要先配置好哨兵模式. 但是在配置哨兵模式时出现了问题.之前没有搭建集群时(一主两从,三台虚拟机)可以顺利配置好,而搭建集群时 ...
- 模拟实现call,apply,bind方法,以及三者区别
// 模拟实现call方法 Function.prototype.call2 = function (context) { var context = context || window; conte ...
- reactnative安装
React Native 介绍 React Native (简称RN)是Facebook于2015年4月开源的跨平台移动应用开发框架,是React 在原生移动应用平台的衍生产物,使用JS.JSX.CS ...
- can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
predict=predict.data.numpy() 这一行报错意思是:如果想把CUDA tensor格式的数据改成numpy时,需要先将其转换成cpu float-tensor随后再转到nump ...
- Vue3+Vite项目中 使用WindiCSS.
之前工作有了解过根据类名来写元素的样式,一听就发出疑问:这样写项目可读性恐怕不是很好吧... 之后来到杭州工作后,开始使用WindiCSS后发现 真香!!! 由于近期所写的项目都是自己一个人开发的 ...
- CatDCGAN项目复现与对抗网络初识
CatDCGAN项目复现与对抗网络初识 作者 CarpVexing 日期 100521 禁止转载 目录 CatDCGAN项目复现与对抗网络初识 引言 CatDCGAN项目基本信息 复现项目的准备工作 ...
- CSS3之伸缩布局
一 主轴方向 在伸缩布局中, 默认伸缩项是从左至右的排版的 主轴的排版的方向默认就是row, 默认就是从左至右 1.默认情况下主轴是水平方向的, 但是也可以修改为垂直方向.只要看到flex-direc ...