Why does Try-Catch require curly braces

前端 未结 10 1149
一整个雨季
一整个雨季 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:25

    Another way of looking at this…

    Given all the maintenance problem that have been created by “if”, “while”, “for” and “foreach” statements without bases, a lot of companies have coding standards that always require bases on statements that act on a “block”.

    So they make you write:

    if (itIsSo)
    {
    ASingleLineOfCode();
    }
    

    Rather then:

    if (itIsSo)
    ASingleLineOfCode();
    

    (Note as indenting is not checked by the compiler, it can't be depended on to be right)

    A good case could be made for designing a language that always require the bases, but then too many people would have hated C# due to having to always use the bases. However for try/catch there was not an expectation of being able to get away without using the bases, so it was possible to require them without to many people complaining.


    Given a choose I would much rather have if/endIf (and while/endWhile) as the block delimiters but the USA got its way on that one. (C got to define what most languages look like rather than Module2, afterall most of what we do is defined by history not logic)

提交回复
热议问题