1

I've made some progress on this thanks to answers to my previous question. That turned out to be a duplicate manifestation of something already covered here. TLDR version: debugging in Win64 behaves "oddly" with Form_Load events.

What I'm trying to do now is find the best way to deal with this, and get VS work properly so that I can actually get down to programming/debugging. There are so many settings, in different places, that it's hard to keep track of them all. I did a full set of tests with every combination of these:

a. Build Platform: Any CPU or x64

b. "Prefer 32-bit" (N/A when x64 selected)

c. Just My Code (enabled/disabled)

d. Break on all thrown exceptions (not just user-unhandled ones)

e. Throw exception in Form.Load or Form.New

Kept constant: Application Framework enabled; startup object is Form1, thus called from this non-user code:

Protected Overrides Sub OnCreateMainForm()
   Me.MainForm = Global.TheNameOfMyApplication.Form1
End Sub

Can't post the full results here as it's long, and I can't find a way to make tables in these posts. But the conclusion is that I see 4 different behaviours from the VS debugger. In order of Good to Bad to Seriously Ugly:

1. Break on actual line that caused the exception. All good: this is "proper" behaviour.

2. Break on the line in OnCreateMainForm (above). Inner exception gives the dirt. No way to get it to break inside the procedure where the exception is thrown (and e.g. look at what's actually happening).

3. Break on a blank (no code) window. At the top it says "No source available: the call stack contains only external code".

4. No break. Exception swallowed. Procedure where exception is thrown is just abandoned; lines after the throw are not executed. Program carries on, whistling a happy tune and hoping I don't notice.

Obviously I want behaviour 1. Here's what I found.

  • The Build Platform and "Prefer 32-bit" settings make absolutely no difference in this test.
  • Enabling Break on thrown exceptions (in Debug/Exceptions dialog) makes everything work properly (Good behaviour 1).
  • Without this enabled, code in Form.Load always results in Ugly behaviour 4 (exception swallowed).
  • Without this enabled, code in Form.New does either of two things. If JustMyCode is enabled, I get Bad behaviour 3 ("No source available"). If not, I get Bad behaviour 2 (break in the call that creates the form - can't get into the New constructor to see what's going on).

I was hoping that coding in Form.New rather than Form.Load (as advised in answer to my previous question) would be a workaround. Turns out that it's not. Unless I'm missing something, the only way I can get debugging to work is to enable Break on Thrown Exceptions. I would really like not to have to do this; among other things, I do want to catch and selectively handle exceptions, and breaking on all thrown exceptions makes this a total PITA to work with.

(Bear in mind that this project only got slightly past the Hello World stage before I ran into this problem - I'm not doing anything funky yet, and really don't want to have a PITA debugger behaviour hanging round my neck when I do).

Effectively I'm having to tick all the "Break on Thrown Exceptions" boxes, as a substitute for the option I'm really looking for:

Just Work Properly Please

I also installed the hotfix mentioned in previous answers, and tried setting up exception handlers in a Sub Main procedure, but that didn't seem to solve the problem either. Is this Break on Thrown Exceptions my best option?

Community
  • 1
  • 1
sebt
  • 525
  • 3
  • 8

2 Answers2

2

The easiest workaround: Use Win8 ...

Division by Zero exception in Form_Load, tested with VS2012 (VB):

Win7x64

AnyCPU : Swallow
x86 : Swallow
x64 : App dies without notice

Win8x64

AnyCPU : Break into debugger
x86 : Break into debugger
x64 : App dies without notice

So if you just want to get it ***ing working, you might consider to test with Win8 ...

igrimpe
  • 1,775
  • 11
  • 12
  • Good answer! But unfortunately I don't have Win8 or the power to get it/use it here at work. Interesting that you just got an "app-death" when building to x64, on either OS. I didn't get that at all. Tried a div/0 in Form_Load - got either an Overflowexception (Break on Thrown exceptions ON), or a swallow (OFF). – sebt Nov 22 '12 at 14:18
  • Might be Microsoft's way to say: "Psssssst .. wanna buy an update?". Might be interesting, if others see the same behavior, because I'm not sure about my Win8 machine. It's an updated Win7 where VS2005/08/10 and 12RC were installed before VS2012RTM. So there is still a chance that there's something I installed over the years, that made it "working" (= breaking) – igrimpe Nov 22 '12 at 14:33
0

I doubt this happens in Form.New but I confirm this happens in Form.Load.

Root cause: Form.Load runs on different thread than regular Form events and thus error handling has different behavior there.

Recommendation: avoid Form.Load, move code to Form.New and Form.Show. Using Form.Load is a habit of programmers coming from to , but in the latter, it has completely different behavior (the one native for Windows) what many programmers do not know.

Learn more: good place to start

Community
  • 1
  • 1
miroxlav
  • 11,796
  • 5
  • 58
  • 99