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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This is just a side effect of "lazy loading"... where referenced assemblies are not pre-loaded at the start of the application, and only get loaded when the assembly first used.

I would consider this a good thing, since users may not use a feature of an app that required the DLL, so why would the app load something that isn't used.

You can manually control some of this behavior using the AssemblyResolve feature
MikeBroderickAuthor Commented:
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?
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

MikeBroderickAuthor Commented:
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.
You need to handle exceptions in your code. If you know how to fix your code to not throw errors then thats better, however some things always need exception handling because you can't always guarentee the output of the behavior. The best example of this would be trying to access a file a user doesn't have permissions to access, although some users have access others don't and your code needs to handle the scenerios in which an exception would occur based on insufficient privileges.

Best Practices for Handling Exceptions
Dim x, y As Int32
   x = x / y
   x = 1 ' this statement will not execute
 Catch ex As Exception
 End Try
 MessageBox.Show("I executed still")

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MikeBroderickAuthor Commented:
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

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?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.