如何强制每个Inside方法必须抛出

时间:2018-08-01 06:33:13

标签: c#

强制每个Inside方法必须抛出或强制不使用try catch块。

下面的示例InsideMethod2()throw方法中被调用时,Main()并不是强制性的,应给出警告或错误消息,说明您必须throw或不应使用try catch块。

示例:

public void Main()
{
    try
    {
        InsideMethod1();
        InsideMethod2();
    }
    catch (System.Exception)
    {
        throw;
    }
}

public int InsideMethod1()
{
    try
    {
        // implementation here
    }
    catch (System.Exception)
    {
        // log
        throw;
    }
}

public int InsideMethod2()
{
    // enforce throw or enforce not use try catch block
    int a = 1, b = 0, c = 0;
    try
    {
        c = a / b;
    }
    catch (System.Exception)
    {
        // enforce to throw
        // log
    }

    return c;
}

是否可以强制执行内部方法??

0 个答案:

没有答案