C#:为什么要费心使用“finally”?条款?

发布于 2024-09-13 05:11:40 字数 415 浏览 7 评论 0原文

可能的重复:
为什么在 C# 中使用finally?

在 C# 中,使用 finally 子句?

例如。

try {
        // do something
    }
catch (Exception exc)
    {
        // do something
    }
// do something

最后的代码不会执行吗? finally 块有什么意义?

Possible Duplicate:
Why use finally in C#?

In C#, what is the point of having a finally clause?

eg.

try {
        // do something
    }
catch (Exception exc)
    {
        // do something
    }
// do something

Won't the code at the end execute anyway? What is the point of a finally block?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

忘你却要生生世世 2024-09-20 05:11:41

finally 确保块中的任何内容都将被执行,无论前两条语句是否被激活。

一个很好的例子是释放数据库资源。

尝试-捕获-最终

示例:

try
{
   //Open a database connection
}
catch
{
   //Catch exception, database connection failed
}
finally
{
   //Release the opened database connection resources
}

The finally ensures that anything in the block will be executed, regardless if the previous two statements are activated.

A good example would be to release database resources.

Try-Catch-Finally

Example:

try
{
   //Open a database connection
}
catch
{
   //Catch exception, database connection failed
}
finally
{
   //Release the opened database connection resources
}
弄潮 2024-09-20 05:11:41

无论之前的代码是否成功,您都希望执行某些代码。通过使用 Try/Catch/Finally,您可以从错误处理中受益。

There are pieces of your code that you want to execute regardless of the success of previous code. By using Try/Catch/Finally you are able to benefit from the error handling.

别念他 2024-09-20 05:11:41

因为您应该对某些对象执行清理,否则会给您的应用程序带来问题。

经常使用的示例是使用 SqlConnection:

SqlConnection conn = new SqlConnection(connString);

try
{        
    conn.Open();

    throw new ArgumentException();
}
catch(SqlException ex)
{
}

在这种情况下,SqlConnection 保持打开状态,无法关闭它,因为您处理了 SqlException,但抛出了 ArgumentException。如果您使用finally块,则不会发生这种情况,因为finally块代码将执行:

try
{        
    conn.Open();

    throw new ArgumentException();
}
catch(SqlException ex)
{
}
finally
{
    conn.Dispose();
}

Because there are certain objects that you should perform cleanup on, otherwise it will cause issues for your application.

The constantly used example is using a SqlConnection:

SqlConnection conn = new SqlConnection(connString);

try
{        
    conn.Open();

    throw new ArgumentException();
}
catch(SqlException ex)
{
}

In this case, the SqlConnection is left open with no way of closing it because you handle a SqlException, but ArgumentException is thrown. If you used a finally block, this wouldn't happen because the finally block code would execute:

try
{        
    conn.Open();

    throw new ArgumentException();
}
catch(SqlException ex)
{
}
finally
{
    conn.Dispose();
}
薄荷→糖丶微凉 2024-09-20 05:11:41

当捕获异常时,finally 块保证运行,即使异常处理块抛出更多异常。通常用于清理某些类型的资源,例如打开的文件句柄、网络连接等...

这里有很好的例子:

http: //dotnetperls.com/finally

http://www.csharp-station .com/Tutorials/lesson15.aspx

The finally block is garaunteed to run when an exception is caught, even if the exception handling block throws more exceptions. Often used for cleaning up certain kinds of resources such as open file handles, network connections, etc...

Good examples here:

http://dotnetperls.com/finally

http://www.csharp-station.com/Tutorials/lesson15.aspx

殤城〤 2024-09-20 05:11:41

Final 确保无论尝试成功还是失败,其中的任何内容都会被触发。在发生捕获的情况下,您不能保证最后 //do some 处的所有内容都会发生。

The Finally ensures that whatever is in it will fire if the try works or fails. In a scenario where a catch occurs you are not guaranteed that everything at the last //do something will occur.

莳間冲淡了誓言ζ 2024-09-20 05:11:41

无论是否抛出异常,Finally 块中的代码始终都会执行。如果路径在 catch 中返回,则异常块后面的代码可能会执行,也可能不会执行。

The code in the Finally block always executes, regardless of if an exception was thrown or not. The code following the exception block may or may not execute if the path returns in the catch.

叫嚣ゝ 2024-09-20 05:11:40

finally 是针对即使 catch 抛出异常的情况,加上它允许您在成功和失败时执行可爱的代码,finally 总是会被执行。总是。

好的,除非应用程序在系统级别被终止或计算机爆炸。

Finally is for the event that even the catch throws an exception, plus it allows you to exewcute code on success and failure, the finally will ALWAYS be executed. ALWAYS.

Ok, except when the application is killed at the system level or the computer explodes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文