空的try/catch是否等于捕获异常?

时间:2022-10-28 13:27:22
try {
}
catch (Exception) {
}

can i just write

我可以只写

try {
}
catch {
}

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

这在c# . net 3.5中可以吗?代码看起来更好,但我不知道是否相同。

8 个解决方案

#1


13  

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...
}

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.

此外,如果您可以使用第一种形式捕获特定的异常(例如IOException),那么捕获通用异常类通常是一种糟糕的做法。

#2


30  

They are not the same.

他们不一样。

catch (Exception) { } will catch managed exceptions only; catch { } will catch non-CLS exceptions as well: http://msdn.microsoft.com/en-gb/bb264489.aspx

catch (Exception){}只能捕获托管异常;catch{}也将捕获非cls异常:http://msdn.microsoft.com/en-gb/bb264489.aspx

An unhandled non-CLS compliant exception becomes a security issue when previously allowed permissions are removed in the catch block. Because non-CLS compliant exceptions are not caught, a malicious method that throws a non-CLS compliant exception could run with elevated permissions.

当在catch块中删除先前允许的权限时,未处理的与cls兼容的异常将成为安全问题。由于没有捕获与cls兼容的异常,因此抛出与cls兼容的异常的恶意方法可以运行在更高的权限下。

Edit: Turns out .NET 2.0+ wraps the values -- so they are the same. That's a bit of a relief!

编辑:原来。net 2.0+封装了这些值——所以它们是一样的。这让人松了一口气!

#3


12  

Edit: As of C# 2.0, non-CLS-compliant exceptions can be caught in both ways.

编辑:从c# 2.0开始,不兼容cls的异常可以通过两种方式捕获。

So, yes. They are identical. A parameter-less catch clause without a Type declaration catches all Exceptions.

所以,是的。他们是相同的。没有类型声明的无参数捕获子句捕获所有异常。

In the CLR 2.0, MS introduced RuntimeWrappedException, which is a CLS-compliant exception type, to encapsulate non-CLS-compliant exceptions. The C# compiler still doesn't allow you to throw them, but it can catch them with the catch (Exception) { } syntax.

在CLR 2.0中,MS引入了RuntimeWrappedException(这是一个cls兼容的异常类型)来封装不兼容CLR的异常。c#编译器仍然不允许您抛出它们,但是它可以使用catch (Exception){}语法捕获它们。

This is why the C# compiler will issue warning CS1058 if you use both clauses at the same time on CLR 2.0 or later.

这就是为什么如果您在CLR 2.0或更高版本上同时使用这两个子句,c#编译器将发出警告CS1058。

Thus, they are in fact identical.

因此,它们实际上是相同的。

#4


1  

Its the same, but if you put an e after Exception in your first example then you know what exception was thrown...

这是一样的,但是如果你在第一个例子中加入一个e,然后你就知道抛出了什么异常……

edit: you should never catch exception, how do you know how to handle it properly?

编辑:您不应该捕捉异常,您如何知道如何正确处理它?

#5


1  

I guess unless you want to use the Exception in some sort the second one is perfectly fine. though in order to use the exception in first one you need to declare a variable like this

我猜除非你想用某种例外情况第二种完全没问题。虽然为了在第一个中使用异常,您需要像这样声明一个变量

try {
}
catch (Exception e) {
//do something with e
}

#6


0  

Both of your examples appear like you're not doing anything with the exception data. This is generally not a good practice. But both are exactly the same since all exceptions classes are derived from System.Exception.

您的两个示例似乎都没有处理异常数据。这通常不是一个好的练习。但是这两个都是完全相同的,因为所有的异常类都是从System.Exception派生的。

You should consider doing some type of logging then possibly rethrow the original exception or wrap it in a more specialized exception that your application can understand.

您应该考虑进行某种类型的日志记录,然后可能重新抛出原始异常,或者将其封装到应用程序可以理解的更专门化的异常中。

try
{
    // Some code here
}
catch(Exception ex)
{
    // Do some logging
    throw;
}

OR

try
{
    // Some code here
}
catch(Exception ex)
{
    // Do some logging
    // wrap your exception in some custom exception
    throw new CustomException("Some custom error message, ex); 
}

You should typically only catch exceptions that your code could handle, otherwise it should bubble up and it should eventually be caught by a global exception handler assuming you have one.

通常,您应该只捕获您的代码可以处理的异常,否则它将冒泡,并最终被一个全局异常处理程序捕获,假设您有一个异常处理程序。

#7


0  

They are different as noted:

如前所述,它们是不同的:

An unhandled non-CLS compliant exception becomes a security issue when previously allowed permissions are removed in the catch block. Because non-CLS compliant exceptions are not caught, a malicious method that throws a non-CLS compliant exception could run with elevated permissions.

当在catch块中删除先前允许的权限时,未处理的与cls兼容的异常将成为安全问题。由于没有捕获与cls兼容的异常,因此抛出与cls兼容的异常的恶意方法可以运行在更高的权限下。

You can see the difference in the IL generated:

你可以看到IL产生的差异:

//no (Exception)
.try L_0001 to L_0005 catch object handler L_0005 to L_000a

//with (Exception)
.try L_0001 to L_0005 catch [mscorlib]System.Exception handler L_0005 to L_000a

#8


-2  

Parameter less constructor will cause handling of exception types coming from some other languages, exception which are not inherited from c# SYSTEM.EXCEPTION class.

无参数构造函数将导致处理来自其他语言的异常类型,而这些异常不是继承自c#系统的。异常类。

#1


13  

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...
}

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.

此外,如果您可以使用第一种形式捕获特定的异常(例如IOException),那么捕获通用异常类通常是一种糟糕的做法。

#2


30  

They are not the same.

他们不一样。

catch (Exception) { } will catch managed exceptions only; catch { } will catch non-CLS exceptions as well: http://msdn.microsoft.com/en-gb/bb264489.aspx

catch (Exception){}只能捕获托管异常;catch{}也将捕获非cls异常:http://msdn.microsoft.com/en-gb/bb264489.aspx

An unhandled non-CLS compliant exception becomes a security issue when previously allowed permissions are removed in the catch block. Because non-CLS compliant exceptions are not caught, a malicious method that throws a non-CLS compliant exception could run with elevated permissions.

当在catch块中删除先前允许的权限时,未处理的与cls兼容的异常将成为安全问题。由于没有捕获与cls兼容的异常,因此抛出与cls兼容的异常的恶意方法可以运行在更高的权限下。

Edit: Turns out .NET 2.0+ wraps the values -- so they are the same. That's a bit of a relief!

编辑:原来。net 2.0+封装了这些值——所以它们是一样的。这让人松了一口气!

#3


12  

Edit: As of C# 2.0, non-CLS-compliant exceptions can be caught in both ways.

编辑:从c# 2.0开始,不兼容cls的异常可以通过两种方式捕获。

So, yes. They are identical. A parameter-less catch clause without a Type declaration catches all Exceptions.

所以,是的。他们是相同的。没有类型声明的无参数捕获子句捕获所有异常。

In the CLR 2.0, MS introduced RuntimeWrappedException, which is a CLS-compliant exception type, to encapsulate non-CLS-compliant exceptions. The C# compiler still doesn't allow you to throw them, but it can catch them with the catch (Exception) { } syntax.

在CLR 2.0中,MS引入了RuntimeWrappedException(这是一个cls兼容的异常类型)来封装不兼容CLR的异常。c#编译器仍然不允许您抛出它们,但是它可以使用catch (Exception){}语法捕获它们。

This is why the C# compiler will issue warning CS1058 if you use both clauses at the same time on CLR 2.0 or later.

这就是为什么如果您在CLR 2.0或更高版本上同时使用这两个子句,c#编译器将发出警告CS1058。

Thus, they are in fact identical.

因此,它们实际上是相同的。

#4


1  

Its the same, but if you put an e after Exception in your first example then you know what exception was thrown...

这是一样的,但是如果你在第一个例子中加入一个e,然后你就知道抛出了什么异常……

edit: you should never catch exception, how do you know how to handle it properly?

编辑:您不应该捕捉异常,您如何知道如何正确处理它?

#5


1  

I guess unless you want to use the Exception in some sort the second one is perfectly fine. though in order to use the exception in first one you need to declare a variable like this

我猜除非你想用某种例外情况第二种完全没问题。虽然为了在第一个中使用异常,您需要像这样声明一个变量

try {
}
catch (Exception e) {
//do something with e
}

#6


0  

Both of your examples appear like you're not doing anything with the exception data. This is generally not a good practice. But both are exactly the same since all exceptions classes are derived from System.Exception.

您的两个示例似乎都没有处理异常数据。这通常不是一个好的练习。但是这两个都是完全相同的,因为所有的异常类都是从System.Exception派生的。

You should consider doing some type of logging then possibly rethrow the original exception or wrap it in a more specialized exception that your application can understand.

您应该考虑进行某种类型的日志记录,然后可能重新抛出原始异常,或者将其封装到应用程序可以理解的更专门化的异常中。

try
{
    // Some code here
}
catch(Exception ex)
{
    // Do some logging
    throw;
}

OR

try
{
    // Some code here
}
catch(Exception ex)
{
    // Do some logging
    // wrap your exception in some custom exception
    throw new CustomException("Some custom error message, ex); 
}

You should typically only catch exceptions that your code could handle, otherwise it should bubble up and it should eventually be caught by a global exception handler assuming you have one.

通常,您应该只捕获您的代码可以处理的异常,否则它将冒泡,并最终被一个全局异常处理程序捕获,假设您有一个异常处理程序。

#7


0  

They are different as noted:

如前所述,它们是不同的:

An unhandled non-CLS compliant exception becomes a security issue when previously allowed permissions are removed in the catch block. Because non-CLS compliant exceptions are not caught, a malicious method that throws a non-CLS compliant exception could run with elevated permissions.

当在catch块中删除先前允许的权限时,未处理的与cls兼容的异常将成为安全问题。由于没有捕获与cls兼容的异常,因此抛出与cls兼容的异常的恶意方法可以运行在更高的权限下。

You can see the difference in the IL generated:

你可以看到IL产生的差异:

//no (Exception)
.try L_0001 to L_0005 catch object handler L_0005 to L_000a

//with (Exception)
.try L_0001 to L_0005 catch [mscorlib]System.Exception handler L_0005 to L_000a

#8


-2  

Parameter less constructor will cause handling of exception types coming from some other languages, exception which are not inherited from c# SYSTEM.EXCEPTION class.

无参数构造函数将导致处理来自其他语言的异常类型,而这些异常不是继承自c#系统的。异常类。