在 C# 中,为什么在 finally 块的开头没有明确分配变量? [英] In C#, why is a variable not definitely assigned at the beginning of a finally block?

查看:23
本文介绍了在 C# 中,为什么在 finally 块的开头没有明确分配变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么下面的代码会产生错误.通常我可以从语言规范中弄清楚,但在这种情况下,我不了解语言规范.

I don't understand why the following code produces an error. Normally I can figure things out from the language specification, but in this case I don't understand the language specification.

这不会导致我的代码出现问题,顺便说一句,我只是想了解语言.

This isn't causing problems in my code, by the way, I just want to understand the language.

示例:

bool success;
try
{
    success = true;
}
catch
{
    success = false;
}
finally
{
    Console.WriteLine(success); // ERROR: Local variable 'success' might not be initialized before accessing
}

这种行为似乎适用于所有版本的 C#,但下面的引用来自 C# 语言规范 5.0.

This behavior appears to be true of all versions of C#, but the quotes below are from C# Language Specification 5.0.

第 5.3.3.14 节 Try-finally 语句

Section 5.3.3.14 Try-finally statements

finally块开头v的明确赋值状态与stmt开头v的明确赋值状态相同.

The definite assignment state of v at the beginning of finally-block is the same as the definite assignment state of v at the beginning of stmt.

这里的stmt 的开始"是指整个 try-finally 语句的开始,即在 try 之前.

Here "beginning of stmt" refers to the beginning of the entire try-finally statement, i.e. just before try.

第 5.3.3.15 节 Try-catch-finally 语句

Section 5.3.3.15 Try-catch-finally statements

以下示例演示了 try 语句(第 8.10 节)的不同块如何影响确定赋值.

The following example demonstrates how the different blocks of a try statement (§8.10) affect definite assignment.

static void F() {
    int i, j;
    try {
        goto LABEL;
        // neither i nor j definitely assigned
        i = 1;
        // i definitely assigned
    }
    catch {
        // neither i nor j definitely assigned
        i = 3;
        // i definitely assigned
    }
    finally {
        // neither i nor j definitely assigned
        j = 5;
        // j definitely assigned
    }
    // i and j definitely assigned
  LABEL:;
    // j definitely assigned
}

谁能解释为什么 success(在我的示例中)或 i(在语言规范示例中)没有明确分配在 finally 块的开头?

Can anyone explain why success (in my example) or i (in the language spec example) are not definitely assigned at the beginning of the finally-block?

推荐答案

原因很简单 - 不能保证 trycatch 块中的代码会永远执行, 在 finally 块之前.

Simple reason is - There is no guarantee that the code in try or catch block will ever execute, before finally block.

ThreadAbort 异常可能发生在 try 块内,但在赋值执行之前.

ThreadAbort Exception can happen inside the try block, but before assignment executes.

运行时代码在抛出异常之后但在 catch 块中的代码执行之前执行(在 .Net 或结构化异常处理"中搜索异常处理的工作原理).

Runtime code executes after exception is thrown but before code in catch blocks executes (Search for how exception handling works in .Net or "Structured Exception Handling").

因此,在执行 finally 块之前,try 和 catch 块中的代码可能永远不会执行.

Hence, code in try and catch block may never execute, before execution of finally block.

这篇关于在 C# 中,为什么在 finally 块的开头没有明确分配变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆