Results 1 to 14 of 14

Thread: Handled Exception

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Handled Exception

    Hi,

    In a parent subroutine with a Try/Catch I want to check if a file is open.
    If the file is open then it seems to create an exception in the ?parent? routine which means that it doesn't continue with the code.
    Is there any way to handle the exception in the subroutine so that it doesn't interfere with the parent?

    And is the a term for a parent subroutine?

    Code:
        Function IsFileOpen(ByRef FileName As String) As Boolean
            Dim blnRetVal As Boolean = False
            Dim fs As FileStream = Nothing
            Try
                fs = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.None)
            Catch ex As Exception
                    blnRetVal = True
                Finally
                    If Not IsNothing(fs) Then
                        fs.Close()
                    End If
                End Try
            Return blnRetVal
        End Function
    Last edited by sgrya1; Feb 5th, 2019 at 11:30 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Handled Exception

    I found this...

    Code:
    Private Sub IsFileOpen(ByVal file As FileInfo)
        Dim stream As FileStream = Nothing
        Try
            stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)
            stream.Close()
        Catch ex As Exception
    
            If TypeOf ex Is IOException AndAlso IsFileLocked(ex) Then
                ' do something here, either close the file if you have a handle, show a msgbox, retry  or as a last resort terminate the process - which could cause corruption and lose data
            End If
        End Try
    End Sub
    
    Private Shared Function IsFileLocked(exception As Exception) As Boolean
        Dim errorCode As Integer = Marshal.GetHRForException(exception) And ((1 << 16) - 1)
        Return errorCode = 32 OrElse errorCode = 33
    End Function

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Handled Exception

    How your code reacts to an open file depends on the code you run after checking if the file is open. We'd need to see that code...

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Handled Exception

    There's nothing in the parent routine that refers to the file.
    The Exception occurs right at the point of checking if the file is open then instantly returns to the catch of the parent.

    I have a problem with
    Name:  Screenshot_1.png
Views: 191
Size:  8.1 KB

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Handled Exception

    Firstly, there's no justification for declaring that FileName parameter ByRef. For reference types, you only ever use ByRef if you want to assign a new object to the parameter inside the method and have that change affect the original reference. You're not assigning anything to the parameter in the method so there can't possibly be a reason to use ByRef. ByVal is the default for a reason.

    As for your code, it has a number of shortcomings. Firstly, catching any and every exception and declaring that the file is open based on that is wrong. What if a FileNotFoundException is thrown? How does the fact that a file doesn't exist indicate that it is open? Even .paul.'s code is wrong in that it catches an Exception and then tests whether it is type IOException instead of just catching IOException in the first place. Even ignoring that, your code could be improved in a number of ways. This:
    vb.net Code:
    1. If Not IsNothing(fs) Then
    should be this:
    vb.net Code:
    1. If fs IsNot Nothing Then
    but there should be no reason to have to make that check at all. If you open the file with a Using statement then the FileStream will be implicitly closed if it is successfully created:
    vb.net Code:
    1. Private Function IsFileOpen(fileName As String) As Boolean
    2.     Dim result = False
    3.    
    4.     Try
    5.         Using fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None)
    6.         End Using
    7.     Catch ex As Exception
    8.         result = True
    9.     End Using
    10.  
    11.     Return result
    12. End Function
    Last edited by jmcilhinney; Feb 6th, 2019 at 12:24 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Handled Exception

    There's nothing in the parent routine that refers to the file.
    The Exception occurs right at the point of checking if the file is open then instantly returns to the catch of the parent.

    I have a problem with
    Name:  Screenshot_1.png
Views: 184
Size:  8.1 KB

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Handled Exception

    Quote Originally Posted by sgrya1 View Post
    There's nothing in the parent routine that refers to the file.
    The Exception occurs right at the point of checking if the file is open then instantly returns to the catch of the parent.

    I have a problem with
    Name:  Screenshot_1.png
Views: 184
Size:  8.1 KB
    That seems to suggest that you have defined your own class named Exception. That would explain why you're not catching the exception in that method. It is an IOException that is being thrown, which does inherit System.Exception, but you are actually catching something other than System.Exception and so you don't catch the exception that is thrown. In your code, change Exception to System.Exception and I expect that it will work as you expect. In that case, find that class named Exception and get rid of it or, if it actually does serve a useful purpose, rename it to something sensible.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Handled Exception

    jmcilhinney,

    You've helped me many times before. Thank you!!
    I can't check this right now but I'm sure it's the answer.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Handled Exception

    jmcilhinney,

    You've helped me many times before. Thank you!!
    I can't check this right now but I'm sure it's the answer.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Handled Exception

    Arr. Same thing.

    Name:  Screenshot_3.jpg
Views: 478
Size:  13.7 KB

    It says system.IO.IOException so I don't think I've defined my own class for an exception.
    Last edited by sgrya1; Feb 6th, 2019 at 01:19 AM.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Handled Exception

    Quote Originally Posted by sgrya1 View Post
    Arr. Same thing.

    Name:  Screenshot_3.jpg
Views: 478
Size:  13.7 KB

    It says system.IO.IOException so I don't think I've defined my own class for an exception.
    That's a complete non-sequitur. What that is telling you is that a System.IO.IOException was thrown. That class inherits the System.Exception class so, if your code was actually catching System.Exception then it would catch an IOException. The fact that it's not means that you are not catching System.Exception. Nothing you have shown there is any indication that you haven't defined your own Exception class and, in fact, is evidence that you have. As far as I can tell, it's either that or something on your system is broken.

    The way to find out what that class actually is to right-click on it and select Go To Declaration or the like. If you have declared that type in your own code then doing that will show you where. That said, as I already mentioned, you shouldn't be catching System.Exception in the first place. The point of your code is to determine whether a file is already open and it will only be an IOException that gets thrown in that situation so it should be only IOException that you are catching. As I stated quite clearly, catching System.Exception would result in your telling the user that a file that didn't exist was already open, which doesn't seem like any good application should be doing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Handled Exception

    It's possible that the OP has Imported a namespace that has its own Exception class that maybe interfering... so it may not necessarily be that he's defined a custom class Exception. I've been bit by that before.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: Handled Exception

    Quote Originally Posted by sgrya1 View Post
    Name:  Screenshot_3.jpg
Views: 478
Size:  13.7 KB

    You will also get an warning with that code, telling you that a return statement is missing.
    vb.net Code:
    1. Private Function IsFileOpen(fileName As String) As Boolean
    2.     Try
    3.         Using fs = IO.File.Open(fileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
    4.         End Using
    5.  
    6.     Catch ex As Exception
    7.         Return True
    8.  
    9.     End Try
    10.  
    11.     Return False <-----
    12. End Function
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Handled Exception

    Apologies all.
    As jmcilhinney and techgnome has suggested I my project has a reference .dll file that overrules the exception.
    Thank you all for pointing that out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width