Results 1 to 4 of 4

Thread: [2005] A first chance exception of type 'System.InvalidCastException' occurred

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Posts
    362

    Question [2005] A first chance exception of type 'System.InvalidCastException' occurred

    Sometimes, my program generates this kind of exception:
    A first chance exception of type 'System......Exception' occurred in MyCode.exe

    The program does not stop because of it, only shows this exception in debug window. So, I'm not sure where this happened. Is there a way to force the vb express 2005 stop when this exception is happening?

    It seems the program runs like what I want, but this exception worrys me.


    thanks



    bear

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: [2005] A first chance exception of type 'System.InvalidCastException' occurred

    So that means you are executing the code using the IDE. Why would you want VBE 2005 to stop when this exception is occuring?

    The better way of handling this would be to use Exception Handling (Try..Catch..Finally..End try). If you can show us the code which is throing this exception then probably people here will suggest how to do exception handling in your case.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

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

    Re: [2005] A first chance exception of type 'System.InvalidCastException' occurred

    When the IDE says that a first chance exception occurred it means that an exception was thrown. If the IDE doesn't break that means that the exception was caught and dealt with, so there's no issue. Try running this code:
    VB Code:
    1. Try
    2.     IO.File.Open("This file does not exist.", IO.FileMode.Open)
    3. Catch ex As Exception
    4.  
    5. End Try
    and you'll get this output:
    A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
    but there's no need to worry because you've handled the exception.
    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

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

    Re: [2005] A first chance exception of type 'System.InvalidCastException' occurred

    In addition, while they should be avoided if possible, exceptions are a quite legitimate part of program execution. It's their going unhandled that is a real issue, because that means crashy, crashy.
    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

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