[dba-VB] C# - but what happened to catch?

jwcolby jwcolby at colbyconsulting.com
Fri Jul 23 14:51:13 CDT 2010


http://www.jaggersoft.com/pubs/ExceptionHandlingInCSharp.htm

So they work down through this thing and finally declare "it finally works" but where is the catch?

I thought the whole point of a try was to do error handling in a catch?




finally?

One way to solve this problem is to guard the call to reader.Close(). A fourth attempt therefore 
might be:

private static char[] ReadSource(string filename)
{
     TextReader reader = null;
     char[] source;
     try
     {
         FileInfo file = new FileInfo(filename);
         int length = (int)file.Length;
         source = new char[length];
         reader = file.OpenText();
         reader.Read(source, 0, length);
     }
     finally
     {
         if (reader != null)
         {
             reader.Close();
         }
     }
     return source;
}

Of course, the guard on reader.Close() isn't in the "ideal" version of ReadSource. But this is a 
reasonable version if only because it does, finally, work.

Well.. except that there is no catch.

Sigh!

-- 
John W. Colby
www.ColbyConsulting.com



More information about the dba-VB mailing list