0

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:

enter image description here

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?

psubsee2003
  • 8,563
  • 8
  • 61
  • 79
Davide
  • 87
  • 13
  • possible duplicate of [VS2010 does not show unhandled exception message in a WinForms Application on a 64-bit version of Windows](http://stackoverflow.com/questions/4933958/vs2010-does-not-show-unhandled-exception-message-in-a-winforms-application-on-a) – Hans Passant Aug 12 '12 at 11:04
  • Thanks Hans, I'll check that thread out! Sorry I did not find it before posting! – Davide Aug 12 '12 at 11:44
  • It was indeed that same bug, thanks Hans. I'm not sure the question is a duplicate though, as both language (VB vs C#) and IDE (I'm using the free version of VS) are different (which implies, for example, that the first of the proposed solutions in the thread you linked is not possible for me). I solved it by writing an explicit try catch statement. Thanks again! – Davide Aug 12 '12 at 16:21
  • It's a Windows problem, it doesn't have anything to do with the language. – Hans Passant Aug 12 '12 at 16:22
  • Sure! The underlying problem is the same. What I was saying is it makes sense to leave both questions, because if someone looks for "unnotified exception vb.net" or something like that (like I did), they won't find the thread you referred me to. Thanks again! – Davide Aug 12 '12 at 20:00

1 Answers1

0

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):

Try

    mPlayer.Open(filename);

Catch ex As Exception

    'Do something with the exception 
    ' (e.g. write a log, set a value, or display error message)

End Try

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 the Open() 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 catch block (I can't think of one at the moment, but I'm sure there are some very rare uses). An empty catch will 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 Exception message 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 comment

MSDN has a pretty decent summary of exception handling in the .NET languages. I would encourage you to read more on the subject.

Community
  • 1
  • 1
psubsee2003
  • 8,563
  • 8
  • 61
  • 79
  • Thank you so much for your thorough answer! Very kind of you! I'll definitely look at that guide by Microsoft! I'm afraid I actually ran into the bug :-/ Thanks again! – Davide Aug 12 '12 at 11:48
  • I solved it exactly as you explained and your syntax was perfect! – Davide Aug 12 '12 at 16:22