代码之家  ›  专栏  ›  技术社区  ›  Shlomo

为什么Try-Catch需要大括号

  •  49
  • Shlomo  · 技术社区  · 15 年前

    只是好奇:为什么 try catch in C# (Java也?)多个语句的硬编码?为什么语言不允许:

    int i;
    string s = DateTime.Now.Seconds % 2 == 1 ? "1" : "not 1";
    
    try i = int.Parse(s);
    catch i = 0;
    

    这个例子仅用于琐碎的目的。我知道那里有 int.TryParse .

    10 回复  |  直到 15 年前
        1
  •  66
  •   Mark Schultheiss    15 年前

    try {}
    catch (myexcption)
    {}
    catch (myotherexception)
    {}
    finally
    {}
    

    try
    try
    if (iAmnotsane)
    beatMe(please);
    catch (Exception myexception)
    catch (myotherexception)
    logerror("howdy")
    finally
    

        2
  •  50
  •   Eric Lippert    13 年前

    my blog on December 4th, 2012


    try
    {
      try
      {
          XYZ();
      }
      catch(whatever)
      {
         DEF();
      }
    }
    finally
    {
      ABC();
    }
    

        3
  •  8
  •   kidjan    15 年前

    dangling else problem

    if( blah )
        if ( more blah )
            // do some blah
    else
        // no blah I suppose
    

    try
        try
            // some code that throws!
    catch(some blah)
        // which try block are we catching???
    catch(more blah )
        // not so sure...
    finally
        // totally unclear what try this is associated with.
    

    if( blah )
        if( more blah )
            x = blah;
        else
            x = blahblah;
    

    …是编译器如何解释if/if/else块。然而,把你的缩进弄乱并写下:

    if( blah )
        if( more blah )
            x = blah;
    else
        x = blahblah;
    

    …这使得它看起来像else与外部if语句相关联,而实际上由于c约定,它与内部if语句相关联。因此,我认为需要大括号对于解决歧义和防止相当隐蔽的bug有很大的帮助(即使在代码检查期间,这些问题也很容易忽略)。像Python这样的语言没有这个问题,因为缩进和空白很重要。

        4
  •  7
  •   Jukka Matilainen    8 年前

    如果你假设C语言的设计者简单地选择使用与C++相同的语法,那么问题就变成了为什么用单个语句需要的括号在C++中尝试和捕获块。简单的答案是 Bjarne Stroustrup 认为语法更容易解释。

    The Design and Evolution of C++ 斯特劳斯鲁写道:

    “Try关键字是完全冗余的,大括号也是如此,除非在Try块或处理程序中实际使用多个语句。”

    他接着给出了一个不需要try关键字和的示例。然后他写道:

    “但是,我发现这很难解释,因为引入冗余是为了从困惑的用户中节省支持人员。”

    参考文献: 斯特劳斯鲁普,比亚恩(1994)。C++的设计与实现。Addison Wesley。

        5
  •  1
  •   Albin Sunnanbo    15 年前

    我能想到的第一个问题是大括号创建了一个具有自己变量范围的块。

    try
    {
        int foo = 2;
    }
    catch (Exception)
    {
        Console.WriteLine(foo); // The name 'foo' does not exist in the current context
    }
    

    foo

    int foo;
    try
    {
        foo = 2;
    }
    catch (Exception)
    {
        Console.WriteLine(foo); // Use of unassigned local variable 'foo'
    }
    

        6
  •  1
  •   THX-1138    15 年前
    try // 1
    try // 2
      something();
    catch { // A
    }
    catch { // B
    }
    catch { // C
    }
    

    try // 1
    {
        try // 2
            something();
        catch { // A
        }
    }
    catch { // B
    }
    catch { // C
    }
    
    
    try // 1
    {
        try // 2
            something();
        catch { // A
        }
        catch { // B
        }
    }
    catch { // C
    }
    
        7
  •  0
  •   Jake    15 年前

    if(!int.TryParse(s, out i))
     i=0;
    
        8
  •  0
  •   Sachin Shanbhag    15 年前

        9
  •  0
  •   Ian Ringrose    15 年前

    if (itIsSo)
    {
    ASingleLineOfCode();
    }
    

    if (itIsSo)
    ASingleLineOfCode();
    


        10
  •  -3
  •   Will Marcouiller    15 年前