views:

290

answers:

8

Hi

What does it mean in .Net: try-catch block without any Exception as parameter for catch?

+1  A: 

It catches all catchable exceptions. It's usually a bad idea.

Joren
+1  A: 

It means the catch block will catch any exception.

It also means that you can't do anything with the exception object as you don't have a reference to it.

You may use this pattern when you genuinely don't care about anyexception occuring (and don't want to do anything with it), but generally you should avoid this style.

Paolo
+1  A: 

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

This can be useful to perform some action on any error before re-throwing.

Richard
'This can be useful to perform some action on any error before re-throwing.' Isn't that what `finally` does? Does not impede any exception, but allows you to do some handling anyway.
Joren
@Joren: He means more like handling something in an error state. E.g. maybe if you're in a multi-threaded environment signal the other threads to stop because an error occurred. You wouldn't typically want to do that in a finally.
Ian
@Joren: @Ian has it: an action on error, but not on normal return.
Richard
Ah yes, the difference is of course that finally runs regardless of any exceptions. Fair enough then.
Joren
+5  A: 

It's almost the same as catch (Exception ex), if you are not using unmanaged calls, because all exceptions in .NET derive from the Exception class. It is used when you don't need the instance of Exception in your catch block. But catch without Exception as a parameter will catch also unmanaged exceptions, because in other unmanaged languages exceptions may not be derived from the Exception class.

ArsenMkrt
A: 

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

gd047
A: 

It catches every exception, and you don't have access to the exception instance itself. Which, to me, seems usually like a code smell (there are some exceptional cases where this could be considered ok)

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

Razzie
+1  A: 

There are two types of exceptions: CLS compliant, derived from Exception class and non CLS compliant (when any object can be thrown - Int32, DateTime, etc.). catch clause without exception was used prior .net framework 2.0 to catch non CLS compliant exception, but now they are catch and wrapped in RuntimeWrappedException with property WrappedException pointing to thrown object. That's why such code should be avoided in newer versions of framework.

Yurec
A: 

First of all, a little foreword. The CLR allows an instance of any type to be thrown as an exception; for example it is possible to throw System.String or even System.Windows.Forms.Form object. However, the C# compiler allows throwing only Exception-derived objects. So the only way to catch non-CLS-compliant exception was to have an empty catch() block in your code.

Prior to version 2.0 of the CLR non-empty catch blocks (i.e. catch (Exception ...)) were catching CLS-compliant exceptions only. But in version 2.0 of the CLR, Microsoft has introduced a new RuntimeWrappedException class so that when a non-CLS-compliant exception is thrown (from another language, for example) the CLR automatically constructs an instance of the RuntimeWrappedException class. Since then there's no need to have an empty catch blocks (i.e. catch()) anymore because catch (Exception ) would catch all exception anyway.

Hope this sheds some light.

For further information I can address you to Jefrey Richter's great book "CLR via C#", 3rd Edition of which is on sale now.

Igor Korkhov