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

后端 未结 28 2704
时光说笑
时光说笑 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:11

    My thought would be that because something in the try block triggered the exception its namespace contents cannot be trusted - ie referencing the String 's' in the catch block could cause the throw of yet another exception.

提交回复
热议问题