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 块中返回语句
基础教程推荐
- 如果条件可以为空 2022-01-01
- 将数据集转换为列表 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 获取C#保存对话框的文件路径 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
