Why aren't variables declared in “try” in scope in “catch” or “finally”?

后端 未结 28 2574
时光说笑
时光说笑 2020-11-28 03:44

In C# and in Java (and possibly other languages as well), variables declared in a \"try\" block are not in scope in the corresponding \"catch\" or \"finally\" blocks. For e

28条回答
  •  难免孤独
    2020-11-28 04:22

    Because the try block and the catch block are 2 different blocks.

    In the following code, would you expect s defined in block A be visible in block B?

    { // block A
      string s = "dude";
    }
    
    { // block B
      Console.Out.WriteLine(s); // or printf or whatever
    }
    

提交回复
热议问题