I’m using VB 2010 Express and sometimes my code is just ignored, without any error notification. For example, I have this code at the end of the sub that handles me.Load:

counter = counter is a test line. mPlayer is an object from Toub’s sound midi dll, defined like this:
Dim mPlayer As MyMediaPlayer.MyWinPlayer
mPlayer = New MyMediaPlayer.MyWinPlayer()
When the filename contains a valid midi file, it all works and the msgbox’s are displayed. When the midi file is NOT valid, I would like an error to be generated or AudioLength to be NULL or some weird value. Instead, no error is triggered and the debugger just exits the sub.
More in detail: the debugger stops at the first breakpoint. Then, when I click ‘Continue’, the form is displayed, without the debugger ever reaching the other breakpoints or ever producing the MsgBox’s.
Could you please explain how to make VB give me the error?
I’m not familiar with the library you are using but any exception due to an invalid file will originate from the
MyWinPlayer.Open()method (function in vb-speak). So if an exception is thrown, you need to handle that exception inside of a try-catch. If your current sub does not handle the exception, then the exception will bubble up to the calling method and keep going until there is some place that the exception is handled.A try-catch block would be something like this (I don’t write a lot in vb.net anymore, the syntax may not be perfect):
Despite what I have written in the code block above, I would encourage you not to catch a general
Exception, and instead focus on the specific type of exception that could be thrown from theOpen()method. The reason is you only want to handle exceptions that you know how to handle in the current method and let the other exceptions bubble up. You would need to look at the documentation for the library and see what type of exceptions could be throw and include them.Another important thing to mention is unless you have a very good reason, do not use an empty
catchblock (I can’t think of one at the moment, but I’m sure there are some very rare uses). An emptycatchwill just swallow the exception and not do anything with it. It won’t even notify you that there was an exception.Now if you don’t handle any exception, the exception should bubble up all the way to the top of the call stack and the debugger should show an
Unhandled Exceptionmessage and stop there. If it is not doing that then either you are swallowing the exception further down the call stack or you are running into the bug that is mentioned in the possible duplicate Hans Passant mentioned in his commentMSDN has a pretty decent summary of exception handling in the .NET languages. I would encourage you to read more on the subject.