Link to home
Create AccountLog in
Avatar of Mike Broderick
Mike BroderickFlag for United States of America

asked on

visual basic 2010 Exceptions not occuring at startup.

Why is Visual Studio giving me an exception long after I think I should have recieved one?

I have a VS/2010 Windows Forms project with multiple forms. Form1 is a simple menu; click a button and Form 2 loads. The project has a Module defined, containing a class that is defined in a DLL. The routines in this class reference classes in another DLL. The root problem is the referenced-referenced dll was not included in the project's folder, giving a file-not-found exception. My problem is the error should have happened when Form 1 was loading. The classes defined in the module have New subroutines that should have executed, then uncovered the error. Why am I not seeing the error when Form 1 starts up? Projects in VS/2005 do not have this problem; I get an exception at the expected spot at startup.
SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Mike Broderick

ASKER

After looking further, I think I need to restate the problem. I am causing the problem in Form1_Load event handler. The error is occuring there. What happens is Form1 displays (the rest of the Load event handler is not executed), and the exception occurs when the next form is attempted to load.

To test, I divided by zero in Form1_Load. Form1 displays (the rest of the Load event handler did not execute. I did not get a divide error when form 2 was called.

This makes me think that the problem is the form load event handler is catching exceptions. Am I wrong or is there a way to stop that?

That's interesting... can you send a small demo of what you've just described?
Form1_Load ... Handles Form1.Load
   dim x, y as int32
    x = x  / y
    x = 1 ' this statement will not execute
End Sub
That example makes no sense...

Show us how you create the instance of the 2nd form and how you call it from inside the first form, and how you created a divide by zero that didn't occur when you were expecting it.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
For Graye:

Please note that I misstated the problem at first. I thought the problem involved a missing dll that should have shown up at form 1 load, was showing up at form 2 load. I now see that when the problem occurs (cannot find file), the form 1 load event-handler quits executing and form 1 displays as if nothing wrong happened. To eliminate a dll-not-found problem, I tried another type of problem, the classic divide-by-zero problem that never fails to fail. That was what the example is showing. It shows that if an exception, any exception occurs in the form 1 load event-handler, the event-handler stops executing and the form displays as if nothing wrong happened. Again, I apologize for the confusion.

For  MW:

Yes, I tried a try/catch structure and it worked. If nothing else can be done, thats what I will have to do. This seems to be a "feature" of Visual Studio 2010. This project was originally written on VS 2005 then converted to VS 2008. There are many other projects that I would not like to convert this way.

I find it hard to believe that this is correct behavior: For the initial form load event-handler only, if an exception occurs in the code, simply end the event-handler routine and display the form as if nothing went wrong. If there is some way to tell VB to process exceptions on form1_load, please tell me.

Some other environmental notes:
  The machine is 64-bit.
  Project fails on .Net version 4 and version 2.0
Mike,

I have to agree with graye it would be better to show us your code. There is a good chance your code could be changed to not throw an exception but that also depends on what it is your doing some exceptions just can't be avoided without handling them in your code.

Are you using P/Invoke in your code?
Are you attempting to access file that might not exists on the system?

Can you show us your code?