According to this MSDN article, you should not catch general exceptions. I’m sure there’s a stackoverflow question dealing with that, and I understand why it’s not a good practice but I just saw this example on another MSDN article today:
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
Is there any justification to catching a general exception in that example or is it just that they were lazy to write an example catching all the specific exceptions?
In this case, this is an example and is a poor one at that, like many code examples on MSDN.
This should be catching an IO Exception instead of the base class.
The only place it would make sense to catch
Exceptionis in a global exception handler for logging, provided you rethrow.