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 块中返回语句


基础教程推荐
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01