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()
        Try
            ' 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.
            Do
                line = sr.ReadLine()
                Console.WriteLine(Line)
            Loop Until line Is Nothing
            sr.Close()
        Catch E As Exception
            ' Let the user know what went wrong.
            Console.WriteLine("The file could not be read:")
            Console.WriteLine(E.Message)
        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?

RAM
Tuesday, June 03, 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:")
            Console.WriteLine(E.Message)


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...

    Finally
        If not (sr is nothing)
            sr.Close()

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 (www25.brinkster.com/rchildress)
Tuesday, June 03, 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 04, 2003

Guy,

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!!

RAM
Thursday, June 05, 2003

*  Recent Topics

*  Fog Creek Home