What does this mean in .Net: a try-catch block without exception as a parameter to catch?

What does this mean in .Net: a try-catch block without exception as a parameter to catch?

+6
exception try-catch
source share
9 answers

This means that the catch block will catch any exception.

It also means that you cannot do anything with the exception object, since you have no reference to it.

You can use this template if you really do not like any occurrence (and do not want to do anything with it), but in general you should avoid this style.

+4
source share

This is almost the same as catch (Exception ex) if you are not using unmanaged calls, because all exceptions in .NET come from the Exception class. It is used when you do not need an Exception instance in your catch . But catch without an Exception will also catch unmanaged exceptions as a parameter, since in other unmanaged languages, exceptions cannot be received from the Exception class.

+10
source share

This means that you are trying to catch exceptions that are not compatible with the CLS.

As far as I understand, in version 2.0 of the CLR, when the CLS-compatible exception does not match, the CLR automatically creates an instance of the RuntimeWrapped Exception Class and initializes its private field to refer to the object that was actually thrown. In fact, the CLR now turns all CLS-incompatible exceptions into CLS-compatible exceptions.

+4
source share

He catches all the catching exceptions. This is usually a bad idea.

+2
source share

It will catch all exceptions, but you will not have access to the exception object in the catch block.

This can be useful for performing some action on any error before re-metalizing.

+1
source share

There are two types of exceptions: CLS-compatible, derived from the Exception class and not compatible with CLS (when any object can be thrown - Int32, DateTime, etc.). The catch clause, without exception, was used earlier by the .net framework 2.0 to catch a CLS-incompatible exception, but now they are caught and wrapped in a RuntimeWrappedException with the WrappedException property that points to an abandoned object. This is why such code should be avoided in newer versions of the framework.

+1
source share

First of all, a small introduction. CLR allows you to exclude an instance of any type as an exception; for example, you can throw a System.String object or even System.Windows.Forms.Form . However, the C # compiler only allows objects thrown from Exception to be thrown. So the only way to catch an exception other than CLS is to have an empty catch() code in your code.

Prior to CLN version 2.0, non-empty catch blocks (i.e. catch (Exception ...) ) catch only CLS-compatible exceptions. But in version 2.0 of the CLR, Microsoft introduced a new class, RuntimeWrappedException , so that provided that a CLS-compatible exception (for example, from another language), the CLR automatically creates an instance of the RuntimeWrappedException class. Since then, there is no need to have empty catch blocks (i.e. catch() ), because catch (Exception ) will catch all exceptions anyway.

Hope this sheds some light.

For more information, I can contact you at Jefrey Richter: " CLR via C # ", the 3rd edition of which is already on sale.

+1
source share

Although the catch clause can be used without arguments to exclude any type of exception, this use is not recommended. In general, you should catch only those exceptions about which you know how to recover them. Therefore, you should always specify an object argument obtained from System .. ::. Exception

0
source share

It receives each exception, and you do not have access to the exception instance itself. Which, it seems to me, usually resembles the smell of code (there are some exceptional cases when this can be considered normal)

I always find it similar to the On Error Resume Next statement in VB.

0
source share

All Articles