?
Solved

vb.net   PUZZLE  !!  (TRY..CATCH)

Posted on 2004-09-01
11
Medium Priority
?
394 Views
Last Modified: 2010-05-18

                                 WWW.heartwarmingstories.org

And the 25,000 dollar question follows:
(monopoly money, that is....)


Why does program behave differently when running through visual studio, compared to just running the executable?

Here is the program:
------------------------------
1) create new vb.net windows app
2) change properties on project such that main is the starting point for the project, and not form1
3) add the following module code, after adding module1, then add the form code to form1
4) run the project through visual studio and run the exe created, outside the visual studio envioronment... they behave differently....


Module Module1
    Public Sub Main()
        Dim s As Form1

        Try
            s = New Form1
            s.ShowDialog()

        Catch ex As Exception

            MessageBox.Show(ex.Message)
            s.Close()

        End Try
    End Sub

End Module





DO NOT CUT AND PASTE THE WHOLE FORM, FOR FORM1
INSTEAD, DROP A BUTTON ONTO THE FORM, AND THEN ONLY CUT AND PASTE THE BUTTON1 CLICK EVENT BELOW...


Public Class Form1
    Inherits System.Windows.Forms.Form

   '+ windows generated code....   <-- this one you will have to leave alone...


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Throw New Exception("Sorry buddy...  Program has fatal error...")

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class





0
Comment
Question by:kamleshmistry
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11955571
I didn't have time for testing this out.  I was curious what the difference was for you.

Bob
0
 
LVL 4

Expert Comment

by:srcalc
ID: 11955674
they both did the same thing for me... they both worked
0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 600 total points
ID: 11955759
you havnt done Application.Run which sets up your message pump (which is why the button wont show up)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 total points
ID: 11955926
On my system the forms looked the same, the difference was in the way the exception was displayed after clicking the button:

From in the IDE, I got a simple msgbox like dialog with no title, "Sorry buddy...  Program has fatal error..." in the body, and an "OK" button.

From the EXE, I got a much more robust message box:

+------------------------------------------------------------------------------------+
| Form1                                                                                            X |
+------------------------------------------------------------------------------------+
|    /----\    An Unhanled exception has occurred.....                                 |
|   |  X  |                                                                                              |
|    \----/    Sorry buddy... Program has fatal error.                                  |
|                                                                                                          |
|  +---------------+                                                        +---------------+  |
|  |     Details     |                                                         |   Continue    |  |
|  +---------------+                                                         +---------------+ |
|                                                                                                          |
+------------------------------------------------------------------------------------+

I changed Sub Main() to this:

    Public Sub Main()
        Try
            Application.Run(New Form1)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

and the dialog box was different again (stil the same in the IDE though):

+------------------------------------------------------------------------------------+
| Form1                                                                                            X |
+------------------------------------------------------------------------------------+
|    /----\    An Unhanled exception has occurred.....                                 |
|   |  X  |                                                                                              |
|    \----/    Sorry buddy... Program has fatal error.                                  |
|                                                                                                          |
|  +---------------+                              +---------------+     +---------------+ |
|  |     Details     |                              |   Continue    |      |      Quit       |  |
|  +---------------+                              +---------------+     +---------------+ |
|                                                                                                          |
+------------------------------------------------------------------------------------+

Idle_Mind
0
 

Author Comment

by:kamleshmistry
ID: 11956005

I didn't ellaborate enough...

The two different results are obtained when clicking on button1
0
 

Author Comment

by:kamleshmistry
ID: 11956068

The difference between the behavior is that one method exectues code of try catch while the other doesn't

In this case, its just a message-box--no problem

But suppose I wanted to save my file when program crashes?

Suppose I put that in the try-catch...

Problem:  executable displays error message and doesn't process the try-catch

The visual studio version works correctly and processes my try-catch...
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 600 total points
ID: 11956899
why not just use a global exception handler ?

0
 
LVL 8

Assisted Solution

by:wguerram
wguerram earned 400 total points
ID: 11958615
I have seen many problems handling errors in the sub main form

      Try
            s = New Form1
            s.ShowDialog()

        Catch ex As Exception

            MessageBox.Show(ex.Message)
            s.Close()

        End Try

For some reason this is not catching the errors on the form.

Probably it is a bug, not sure if it is documented.
0
 

Author Comment

by:kamleshmistry
ID: 11968738

any work-arounds, anybody?

I tried show instead of showdialog, etc....  nothing seemed to work right....
I need to be able to catch exception for the exe file created (not just within visual studio....)

does 2005 work?  is upgrading expensive?






0
 

Author Comment

by:kamleshmistry
ID: 12047487
Nobody gave workable answer.
Question is being graded and points awarded for trying to help...
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 12047742
sorry I didnt see the last post here ... a global exception handler will do this for you ...

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question