Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

exception handling

Following is code block taken from .NET SDK example

Class Test
    Public Shared Sub Main()
            ' Create an instance of StreamReader to read from a file.
            Dim sr As StreamReader = New StreamReader("TestFile.txt")
            Dim line As String
            ' Read and display the lines from the file until the end
            ' of the file is reached.
                line = sr.ReadLine()
            Loop Until line Is Nothing
        Catch E As Exception
            ' Let the user know what went wrong.
            Console.WriteLine("The file could not be read:")
        End Try
    End Sub
End Class

I am wondering what is the best way to handle exception. Because the above code block work great as long as there is no problem. But what if

1) File is there but no readable
2) File is there and readable to a particular point but could not read any further

How do I know whether error is due to condition #1 or #2? Because here everything is handled in one place. In case of condition #2, sr object is not getting closed in Catch block!!. I know we can put sr.Close() in Finally but what about if the file is not there in the first place and Finally always gets executed and we are trying to close a file which does not exist.

Any thoughts/suggestions?

Tuesday, June 3, 2003

In both cases (1 & 2), an IOException should be thrown from sr.ReadLine().  In this case your Catch block should read...

        Catch e As IOException
          ' write code to handle corrupted file here

        Catch E As Exception
            ' Let the user know what went wrong.
            Console.WriteLine("The file could not be read:")

In the case that file exists but is corrupted,  an IOException will be thrown allowing you to trap that _specific_ exception. 

I'm not sure of a good way to discern between 1 & 2 however.  You could probably just use a variable to tell which line you're on.

As for the Finally block, just do this...

        If not (sr is nothing)

If the file was never successfully opened, you'll have a null reference.  If the file was opened, you'll need to close it.

BTW, instead of relying on the general Exception to catch your file not found situation, try using FileNotFoundException instead.

Rick Childress (
Tuesday, June 3, 2003

In other words, what Rick is saying... "Boy, does that SDK example suck!" 

I was kind of thinking the same thing to myself.  ;)

Guy Incognito
Wednesday, June 4, 2003


Here is the another example

' Get the properties of the 'mySearchResult'.
Dim myResultPropColl As ResultPropertyCollection
myResultPropColl = mySearchResult.Properties
Console.WriteLine("The properties of the 'mySearchResult' are :")
Dim myKey As String
For Each myKey In  myResultPropColl.PropertyNames
  Dim tab1 As String = "    "
  Console.WriteLine(myKey + " = ")
  Dim myCollection As Object
  For Each myCollection In  myResultPropColl(myKey)
      Console.WriteLine(tab1 + myCollection)
  Next myCollection
Next myKey

If you try running this example, it will fail(if you have all the properties set in the AD).  Printing the tab should have been a separate line!! Hey, we are not supposed to find fault in something which is free!!

Thursday, June 5, 2003

*  Recent Topics

*  Fog Creek Home