Hi
What does it mean in .Net: try-catch block without any Exception as parameter for catch?
Hi
What does it mean in .Net: try-catch block without any Exception as parameter for catch?
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.
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.
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.
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
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.
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.
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.