Solved

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

Posted on 2004-09-01
11
386 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
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 150 total points
ID: 11955759
you havnt done Application.Run which sets up your message pump (which is why the button wont show up)
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 150 total points
ID: 11956899
why not just use a global exception handler ?

0
 
LVL 8

Assisted Solution

by:wguerram
wguerram earned 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now