尝试catch为单行if语句

时间:2013-01-08 21:55:46

标签: c# if-statement try-catch

我需要编写一个使用(单行if)语句的语法,但我需要在某种意义上嵌套它:

(表达式1)?

(如果表达式2抛出ArgumentException,则string.empty抛出其他expression2):string.empty

所以基本上我需要弄清楚在c#中单行if if语句中使用try catch的语法(单行,因为我需要在linq到sql select语句中使用它。)

单行if语句我的意思是使用三元运算符if语句而没有任何分号。

5 个答案:

答案 0 :(得分:6)

你正在寻找这样的东西吗?

(/*expression1*/)? (foo()? string.empty: /*expression2*/):string.empty

foo是:

public /*static?*/ bool foo()
{
    try
    {
        //do stuff
    }
    catch(ArgumentException)
    {
        return true
    }
    catch
    {
        return false
    }
    return false;
}

至于将Try / Catch嵌入到Linq to Sql语句中......好吧,我只想说在这种情况下我会尝试重新考虑我的设计。

答案 1 :(得分:3)

这是一个有趣的问题,因为它会让你问:代码块需要用什么标准来标记为“单行”?

显然,编写8行代码然后删除换行符并不会将其转换为一行代码。

这个怎么样:

    public string Expression2()
    {
        return "One does not write maintainable code this way";
    }

    [Test]
    public void TryCatchAsSingleLine()
    {
        bool expr1 = true;


         // Would you say this is one line?
        string result = expr1 
                            ? (new Func<string>(() => 
                                              { 
                                                 try 
                                                 { 
                                                    return Expression2(); 
                                                 } 
                                                 catch 
                                                 { 
                                                    return string.Empty; 
                                                 } 
                                               }
                                               )
                             )() 
                             : string.Empty;
    }

答案 2 :(得分:2)

将此作为单行使用:

Exception e=null; try { expr1; } catch (Exception ex) {e = ex;} if (e is ArgumentException) { expression2; } else { do whatever or not sure what you want to do with string.empty; }

答案 3 :(得分:0)

尝试使用一两个方法调用。

class Test
{
    static void Main(string[] args)
    {
        if (ExpressionOne() && ExpressionTwo() != string.Empty) DoSomething();
    }

    static bool ExpressionOne()
    {
        // check something
    }

    static string ExpressionTwo()
    {
        try
        {
            return ThisMayThrowArgumentExceptionOrReturnAString();
        }
        catch (ArgumentException)
        {
            return string.Empty;
        }
    }

    // ...
}

答案 4 :(得分:0)

C#不是面向行的,因此您可以在一行中编写所有内容,但生成的代码并不总是可读和可维护的。

我会为可能失败的代码编写一个单独的方法。

public string GetResultSafely()
{
    try {
        // Possibly failing
        return ReadStringFromExternalResource();
    } catch() {
       return String.Empty;
    }
}

然后

result = expression1 ? GetResultSafely() : GetAnotherResult();

请注意,抛出异常不应该是标准的处理方式。如果可能,请避免例外。

BAD:

try {
    result = x / y;
} catch {
    Console.WriteLine("Division by zero!");
}

GOOD:

if (y == 0) {
    Console.WriteLine("Division by zero!");
} else {
    result = x / y;
}

但是,有些情况下无法避免例外情况。通常在访问外部资源时。

相关问题