Solved

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

Posted on 2004-09-01
11
389 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

820 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