是一个空的try / catch等于捕获异常?是一个、异常、try、catch

由网友(拿命爱自己 #)分享简介:try {}catch (Exception) {}我可以只写try {}catch {}这是确定在C#.NET 3.5。 code看起来更好,但我不知道是否是一样的。Is this ok in C# .NET 3.5. Code looks nicer but I don't know if is t...
try {
}
catch (Exception) {
}

我可以只写

try {
}
catch {
}

这是确定在C#.NET 3.5。 code看起来更好,但我不知道是否是一样的。

Is this ok in C# .NET 3.5. Code looks nicer but I don't know if is the same.

推荐答案

是的,第一种形式的好处是,你可以命名异常变量,然后使用该对象来记录异常详细信息的文件,等...

Yes, the advantage of the first form is that you can name the exception variable and then use the object to log the exception details to file, etc...

try {
}
catch (Exception ex) {
  // Log exception message here...
}

此外,它通常是一个不好的做法赶上普通的异常类,如果使用第一种形式,则可以改为捕获特定的异常(如一个IOException)。

Also, it is generally a bad practice to catch the generic Exception class if you can instead catch specific exceptions (such as an IOException) using the first form.

阅读全文

相关推荐

最新文章