c# - 将catch块中的2个语句合并为一个

dcp*_*dcp 1 c# exception

有人知道是否可以将下面的catch块中的代码编写为单个语句?我无法想出办法,如果有的话,我只是很好奇.

重要信息:必须保留堆栈跟踪.

    catch (Exception e)
    {
        if (e is MyCustomException)
        {
            // throw original exception
            throw;
        }

        // create custom exception
        MyCustomException e2 =
            new MyCustomException(
                "An error occurred performing the calculation.", e);
        throw e2;
    }
Run Code Online (Sandbox Code Playgroud)

Mat*_*ias 15

关于什么:

catch (MyCustomException)
{
   throw;
}
catch (Exception ex)
{
   throw new MyCustomException(ex);
}
Run Code Online (Sandbox Code Playgroud)

  • 但是,它看起来比其他人好多了.代码长度不是你应该注意的唯一事情. (4认同)