[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2004-09-01
11
Medium Priority
?
395 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

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!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

650 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