Why aren't variables declared in "try" in scope in "catch" or "finally"? Why aren't variables declared in "try" in scope in "catch" or "finally"? java java

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


Two things:

  1. Generally, Java has just 2 levels of scope: global and function. But, try/catch is an exception (no pun intended). When an exception is thrown and the exception object gets a variable assigned to it, that object variable is only available within the "catch" section and is destroyed as soon as the catch completes.

  2. (and more importantly). You can't know where in the try block the exception was thrown. It may have been before your variable was declared. Therefore it is impossible to say what variables will be available for the catch/finally clause. Consider the following case, where scoping is as you suggested:

    try{    throw new ArgumentException("some operation that throws an exception");    string s = "blah";}catch (e as ArgumentException){      Console.Out.WriteLine(s);}

This clearly is a problem - when you reach the exception handler, s will not have been declared. Given that catches are meant to handle exceptional circumstances and finallys must execute, being safe and declaring this a problem at compile time is far better than at runtime.


How could you be sure, that you reached the declaration part in your catch block? What if the instantiation throws the exception?


Traditionally, in C-style languages, what happens inside the curly braces stays inside the curly braces. I think that having the lifetime of a variable stretch across scopes like that would be unintuitive to most programmers. You can achieve what you want by enclosing the try/catch/finally blocks inside another level of braces. e.g.

... code ...{    string s = "test";    try    {        // more code    }    catch(...)    {        Console.Out.WriteLine(s);    }}

EDIT: I guess every rule does have an exception. The following is valid C++:

int f() { return 0; }void main() {    int y = 0;    if (int x = f())    {        cout << x;    }    else    {        cout << x;    }}

The scope of x is the conditional, the then clause and the else clause.