Return statements in catch blocks(在 catch 块中返回语句)
问题描述
我看到一些开发人员在 catch 块中使用 return 语句.为什么/何时这是一种有用的技术?
I have seen some developers use the return statement in a catch block. Why/when would this be a useful technique to employ?
实际上我刚刚看到使用了 return 关键字.
I actually just saw the return keyword being used.
谢谢
推荐答案
public void Function(){
public void Function() {
try
{
//some code here
}
catch
{
return;
}
}
返回时;命中,执行流程跳出函数.这只能在 void 方法上完成.
when return; is hit, the execution flow jumps out of the function. This can only be done on void methods.
如果您不想执行其余功能,请执行此操作.例如,如果您正在执行文件 IO 并且发生读取错误,则您不想执行处理该文件中数据的代码,因为您没有它.
you do this if you dont want to execute the rest of the function. For example if you are doing file IO and a read error happens, you dont want to execute code that handles processing the data in that file since you dont have it.
这篇关于在 catch 块中返回语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 catch 块中返回语句


基础教程推荐
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01