简体   繁体   English

这个try-catch块是否有效?

[英]Is this try-catch block valid?

Newby question... 新问题......

Is it valid to do: 是否有效:

try
{
    // code which may fail
}
catch
{
    Console.Writeline("Some message");
}

Or do I always have to use: 或者我总是必须使用:

try
{
    // code which may fail
}
catch (Exception e)
{
    Console.Writeline("Some message");
}

Both blocks are valid. 两个块都有效。

The first will not have an exception variable. 第一个不会有异常变量。

If you are not going to do anything with the exception variable but still want to catch specific exceptions, you can also do: 如果您不打算对异常变量执行任何操作但仍希望捕获特定异常,则还可以执行以下操作:

try 
{
   // your code here
}
catch(SpecificException)
{
   // do something - perhaps you know the exception is benign
}

However, for readability I would go with the second option and use the exception variable. 但是,为了便于阅读,我将使用第二个选项并使用异常变量。 One of the worst things to do with exceptions is swallow them silently - at the minimum, log the exception. 与异常相关的最糟糕的事情之一是静默地吞下它们 - 至少记录异常。

Yep, absolutely, such a catch block called general catch clause, see more interesting details in the C# Language Specification 4.0, 8.10 The try statement: 是的,绝对是这样一个名为general catch子句的catch块,在C#语言规范4.0,8.10中看到更多有趣的细节try语句:

A catch clause that specifies neither an exception type nor an exception variable name is called a general catch clause. 既未指定异常类型也未指定异常变量名称的catch子句称为常规catch子句。 A try statement can only have one general catch clause, and if one is present it must be the last catch clause try语句只能有一个常规catch子句,如果有一个,它必须是最后一个catch子句

Yes, your first block of code valid. 是的,您的第一个代码块有效。 It will catch all exceptions. 它将捕获所有异常。

It is. 它是。 It will catch all the exception. 它将捕获所有异常。 So the two code examples do the same. 所以这两个代码示例也是一样的。

First one is valid, and it acts just like the second one. 第一个是有效的,它的行为就像第二个一样。

http://msdn.microsoft.com/en-us/library/0yd65esw%28v=vs.80%29.aspx http://msdn.microsoft.com/en-us/library/0yd65esw%28v=vs.80%29.aspx

The catch clause can be used without arguments, in which case it catches any type of exception, and referred to as the general catch clause. catch子句可以不带参数使用,在这种情况下,它捕获任何类型的异常,并称为general catch子句。 It can also take an object argument derived from System.Exception, in which case it handles a specific exception. 它还可以采用从System.Exception派生的对象参数,在这种情况下,它处理特定的异常。

As @David answered this is valid. 正如@David所说,这是有效的。
You could use second syntax if you want to get more infos or catch a specific exception. 如果要获取更多信息或捕获特定异常,可以使用第二种语法。 Eg 例如

catch (Exception e) 
{
  Debug.Print(e.Message);
}

Yes it is valid. 是的,它是有效的。

you can always refer to this article: 你可以随时参考这篇文章:

Best Practices for Handling Exceptions on MSDN 在MSDN上处理异常的最佳实践

Of course it is valid, you specify catch(Exception e) when you want to output the error message ex.Message, or to catch a custom or a concrete Exception. 当然它是有效的,当你想输出错误消息ex.Message时,你指定catch(Exception e) ,或者捕获自定义或具体的异常。 Use catch in your situation. 在你的情况下使用catch

catch (Exception e)
{
    Console.Writeline("Some message");
}

In this block you can use SqlException, etc.. 在这个块中你可以使用SqlException等。

catch (SqlException e)
{
    Console.Writeline("Some message");
}

For this use the "(SqlException e)" 为此使用“(SqlException e)”

If you will use a generic menssage, use this: 如果您将使用通用的menssage,请使用:

catch
{
    Console.Writeline("Some message");
}

or

catch (Exception)
{
    Console.Writeline("Some message");
}

Don't forget that you can chain catch your exceptions. 不要忘记你可以链接捕获你的异常。 This will allow you to handle different scenarios based upon the exception(s) the code may throw. 这将允许您根据代码可能抛出的异常处理不同的方案。

try
{
    //Your code.
}
catch(SpecificException specificException)
{
    //Handle the SpecificException
}
catch(AnotherSpecificException anotherSpecificException)
{
    //Handle AnotherSpecificException
}
catch(Exception exception)
{
    //Handle any Exception
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM