Results 1 to 10 of 10

Thread: [VB2015] inexplicable unhandled exception in a Try... Catch block

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    459

    [VB2015] inexplicable unhandled exception in a Try... Catch block

    Very very simple code to test a My.Setting value of a "System.Collection.Specialized.StringCollection" item called "ItemChecked" (15 items in the collection).

    Code:
    Private Sub DisplayValue()
    
        Try
    
            Debug.Print(My.Settings.ItemsChecked(0))
    
        Catch ioe As FileNotFoundException
            Debug.Print(ioe.ToString)
            Stop
        Catch ex As Exception
            Debug.Print(ex.ToString)
            Stop
        End Try
    
    End Sub
    Well, when I call the sub I get into the Output window a "Eccezione generata: 'System.IO.FileNotFoundException' in mscorlib.dll" message.

    The exception is Unhandled, since I don't get a "ioe.ToString" or "ex.ToString" data, and program don't stops.

    Inexplicable is the fact that the "Debug.Print(My.Settings.ItemsChecked(0))" code displays without problems the RIGHT ItemsChecked(0) value.

    And then: what has to do a 'System.IO.FileNotFoundException' with a My.Settings value??


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: [VB2015] inexplicable unhandled exception in a Try... Catch block

    If the exception was unhandled then your app would be crashing. If it's not crashing then the exception is being handled. The fact that the exception was thrown doesn't necessarily mean that you can catch it. It was almost certainly thrown AND CAUGHT inside some Framework code. Code is allowed to throw exceptions, catch them and then clean up accordingly. Most likely what you're seeing is the Framework looking for a user config file and not finding one, at which point it probably creates one. User-scoped settings have their default value stored in the app config file and their current value stored in a user config file. Obviously there is no user config file at first so the default value is initially copied into a new user config file. That's most likely what that exception relates to.

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: [VB2015] inexplicable unhandled exception in a Try... Catch block

    I can think of a few situations.

    First, whether you handle it or not, exceptions are thrown and the output window will note it. This is what the debugger is for. It does not mean the exception is unhandled if it's in the output window.

    Second, if you're debugging with Visual Studio, you might be getting a first-chance exception dialog. This is a sometimes nice and sometimes not nice debugging feature. These dialogs appear when the exception is thrown, before .NET checks to see if they are handled. Getting a first-chance exception dialog doesn't mean the exception is unhandled. This might explain why you don't see the expected string in the output window: the program hasn't had a chance to get there yet.

    Third, if a first-chance dialog is appearing, it could be as jmc says: the exception could be being thrown and handled inside Microsoft's code, but a first-chance dialog will display anyway. You don't get to handle it because Microsoft handled it.

    My money's on a combination of (2) and (3): first-chance dialogs are enabled, and this exception is handled in Microsoft's code.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    459

    Re: [VB2015] inexplicable unhandled exception in a Try... Catch block

    Quote Originally Posted by jmcilhinney View Post
    If the exception was unhandled then your app would be crashing.....
    Application don't crashes... but the form where is the above code MUST be owned to an external window. I "attach" it with the code:

    Code:
     NativeMethods.SetWindowLong(Me.Handle, -8, ExternalWindowHwnd)
    After the 'System.IO.FileNotFoundException' the form STOPS to be owned.

    Furthermore Timers stop to work, so that the external application stops to be handled.

    In few words: program don't crashes, but is useless for my goal.

    NOTE:exception rises only if I check the value of a "System.Collection.Specialized.StringCollection" item. All other items are read without any problem.

    Finally: what has to do a 'System.IO.FileNotFoundException' with a code that reads a My.Setting item value??
    Last edited by phil2000; May 17th, 2016 at 05:30 AM.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    459

    Re: [VB2015] inexplicable unhandled exception in a Try... Catch block

    cut...

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: [VB2015] inexplicable unhandled exception in a Try... Catch block

    Settings are stored in the config file ... they are then exposed through a designer file as shared methods in the My.Settings namespace... that's what makes them work. The first time you access one, it needs to load it ... sounds like you're missing your application.exe.config file.

    -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??? *

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    459

    Re: [VB2015] inexplicable unhandled exception in a Try... Catch block

    Quote Originally Posted by techgnome View Post
    ... The first time you access one, it needs to load it ... sounds like you're missing your application.exe.config file...
    The application.exe.config file is present in the folder: the task of one of the fists Subs of the program is precisely to check that all the needed files are present.

    In fact, like I said before, the "Debug.Print(My.Settings.ItemsChecked(0))" code displays without problems the RIGHT ItemsChecked(0) value.

    And before to call the above Sub I check several other My.Settings items. However it's true that the exception rises ONLY FIRST TIME I call the "DisplayValue()" Sub. It's also true that integer, boolean or string items are read without any exception.

    So, I think, the problem comes from the fact that the My.Settings item is a "System.Collection.Specialized.StringCollection" one in a owned form. But I don't understand why...

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: [VB2015] inexplicable unhandled exception in a Try... Catch block

    change this
    Debug.Print(My.Settings.ItemsChecked(0))
    to this
    Dim itmChecked As String = My.Settings.ItemsChecked(0)
    Debug.Print(itmChecked)

    But there's nothing inherent in that code that should be causing an exception to happen...


    -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??? *

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    459

    Re: [VB2015] inexplicable unhandled exception in a Try... Catch block

    Code:
    Private Sub DisplayValue()
    
        Try
    
            Dim bool As Boolean = _
              My.Computer.FileSystem.FileExists(Application.StartupPath & "\MyFile.exe.config")
           
            Debug.Print("File exists is: " & bool.ToString)
            Debug.Print("Now we try again....")
            Dim itmChecked As String = My.Settings.ItemsChecked(0)
            Debug.Print("itmChecked: " & itmChecked)
    
        Catch ioe As FileNotFoundException
            Debug.Print(ioe.ToString)
            Stop
        Catch ex As Exception
            Debug.Print(ex.ToString)
            Stop
        End Try
    
    End Sub
    The output:
    File exists is: True
    Now we try again....
    Eccezione generata: 'System.IO.FileNotFoundException' in mscorlib.dll
    itmChecked: S ' S is the right value
    Inexplicable! Question: how to chek WHAT FILE is not found if I don't Catch the exception??
    Last edited by phil2000; May 17th, 2016 at 11:15 AM.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    459

    Re: [VB2015] inexplicable unhandled exception in a Try... Catch block

    Solved (I have LOST the war but the problem is solved...)

    Removed the damn "System.Collection.Specialized.StringCollection" item in My.Settings and replaced with a String: " S, S, N, S...."

    Then I get my array splitting the string and save the settings joining the array. Some work more... but no exceptions.

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