ADO DOT NET Posted February 15, 2007 Posted February 15, 2007 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:) Quote
Administrators PlausiblyDamp Posted February 15, 2007 Administrators Posted February 15, 2007 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ADO DOT NET Posted February 15, 2007 Author Posted February 15, 2007 Thanks, but for each time that loops runs in For Next, I will need to open a different new file, won't this make problem?:) Quote
MrPaul Posted February 15, 2007 Posted February 15, 2007 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 :) Quote Never trouble another for what you can do for yourself.
Administrators PlausiblyDamp Posted February 15, 2007 Administrators Posted February 15, 2007 Whoops - my bad. You would probably be better of removing the Finally block and closing the file in the Catch block. Catch x As x ... If Not FilePath Is Nothing Then FilePath.Close() End If End Try Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* Nerseus Posted February 15, 2007 *Experts* Posted February 15, 2007 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 Quote "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
mskeel Posted February 15, 2007 Posted February 15, 2007 I second Nerseus' code. That's the general pattern I use for stuff like this. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.