Why does Try-Catch require curly braces

前端 未结 10 1148
一整个雨季
一整个雨季 2020-12-13 23:57

Just curious: Why is the syntax for try catch in C# (Java also?) hard coded for multiple statements? Why doesn\'t the language allow:

int i;
string s = DateT         


        
10条回答
  •  醉酒成梦
    2020-12-14 00:38

    try // 1
    try // 2
      something();
    catch { // A
    }
    catch { // B
    }
    catch { // C
    }
    

    does B catches try 1 or 2?

    I don't think you can resolve this unambiguously, since the snippet might mean:

    try // 1
    {
        try // 2
            something();
        catch { // A
        }
    }
    catch { // B
    }
    catch { // C
    }
    
    
    try // 1
    {
        try // 2
            something();
        catch { // A
        }
        catch { // B
        }
    }
    catch { // C
    }
    

提交回复
热议问题