Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

   Private Sub ProcessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessButton.Click
       Try
           Dim FilePath As FileStream = Nothing
           For Counter As Integer = 1 To Some Number
               FilePath = New FileStream(FileString, FileMode.Create, FileAccess.ReadWrite)
               ...process
               FilePath.Close()
           Next
       Catch x As x
           ...
       Finally
           FilePath.Close()
       End Try
   End Sub

The above code is general structure of my code, as you can see there, I create and use a file stream and close it before each next loop command.

But it is possible that some time due to an error control goes outside of the loop into the catch section without closing the file stream.

So I put a FilePath.Close() in the finally section to make sure that file is closed.

But if no error occurs, I get another error for FilePath.Close() in the finally section! I cannot close an already closed stream.

What should I do for such case? What do you recommend?

Thanks all:)

  • Administrators
Posted

Private Sub ProcessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessButton.Click

Try
   Dim FilePath As FileStream = Nothing

   For Counter As Integer = 1 To Some Number
       FilePath = New FileStream(FileString, FileMode.Create, FileAccess.ReadWrite)

        ...process

    Next
Catch x As x
        ...
Finally
     If Not FilePath Is Nothing Then
         FilePath.Close()
     End If
End Try

End Sub

 

You don't need to close the file within the Try block as the Finally will always run - it is a good idea to check that you have a valid file though just incase the New FileStream(...) bit fails for any reason.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Closing within loop

 

Since you're opening and closing the file within a loop, I expect PlausiblyDamp's alteration won't be exactly what you want. If you set FilePath to Nothing after you close it within the loop, the Finally will not attempt to close it.

 

Try
   Dim FilePath As FileStream = Nothing

   For Counter As Integer = 1 To Some Number
       FilePath = New FileStream(FileString, FileMode.Create, FileAccess.ReadWrite)

       '...process

       FilePath.Close()
       FilePath = Nothing
   Next
Catch x As Exception
   '...
Finally
   If FilePath IsNot Nothing Then
       FilePath.Close()
   End If
End Try

 

Good luck :)

Never trouble another for what you can do for yourself.
  • *Experts*
Posted

Since the code is likely a bit larger than your sample, I'd think that moving the code inside of the loop into a separate function. That separate function is responsible for the file creation/writing/closing. In that function you'd have a try/catch/finally. Something like this:

Private Sub ProcessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessButton.Click
For Counter As Integer = 1 To Some Number
	ProcessFile(FileString)
Next
End Sub
   
Private Sub ProcessFile(ByRef FileString As String)
Dim FilePath As FileStream = Nothing

Try
	FilePath = New FileStream(FileString, FileMode.Create, FileAccess.ReadWrite)
	...process
Catch x As x
	...
Finally
	' If file was opened and created, close it
	If (FilePath Is Not Nothing)
		FilePath.Close()
	End If
End Try
End Sub

 

I left the check against "Nothing" in the finally since there's a chance that the file opening/creation failed and hit the Catch block. You don't want an exception in the Finally block while trying to call Close on a null reference.

 

If you need to know if a file failed so that you won't continue looping, you can have ProcessFile return a bool indicating success/error and check that within the loop - or anything else you want.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...