Solved

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

Posted on 2004-09-01
11
382 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
Comment Utility
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
Comment Utility
they both did the same thing for me... they both worked
0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility

I didn't ellaborate enough...

The two different results are obtained when clicking on button1
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:kamleshmistry
Comment Utility

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
Comment Utility
why not just use a global exception handler ?

0
 
LVL 8

Assisted Solution

by:wguerram
wguerram earned 100 total points
Comment Utility
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
Comment Utility

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
Comment Utility
Nobody gave workable answer.
Question is being graded and points awarded for trying to help...
0
 
LVL 37

Expert Comment

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

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

11 Experts available now in Live!

Get 1:1 Help Now