There's no difference between the first and the second block, because all exceptions inherit from
Exception
. If you change
Exception
into another exception,
OutOfMemoryException
for example, then the code in the catch block will only be executed if the exception which is thrown a
OutOfMemoryException
is.
In the third block, the exception which is thrown is also stored in a variable, so you can get the error message for example:
try
{
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
[EDIT]
Kaizen202 wrote:
In my program, I need to catch all exceptions and I don't want to log them. From the above mentioned Try-Catch blocks, which should be used?
If you don't need to specify a exception type, and if you don't want to log them, then use
try
{
}
catch
{
}
And if you need to specify an exception type, but if you don't want to log them, then use
try
{
}
catch (OutOfMemoryException)
{
}
But if you need to log them, then use
catch (Exception ex)
Hope this helps.