Hello

In my code I am trying to come up with the following messages using a try/catch

Unhandled exception has occurred in your application. If you click continue, the application will ignore this error adn attemp to continue. If you click Quit, the application will close immediately.

conversion from string """" to type 'integer' is not valid

Warning Icon

followed by 3 buttons one that saids the DETAILS, another for CONTINUE, and one for QUIT.

Here is part of my code. I did use Try/catch correctly, but it doesnt say the message the way i described it above ..

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim rtype As String
        Dim run As fencerun

        rtype = TypeCode()

        Try

            If fencerun.IsRun(rtype, CInt(txtLength.Text)) Then
                run = New fencerun(rtype, CInt(txtLength.Text))
                fence.Add(run)
                'MessageBox.Show(run.Type & run.Feet.ToString)
                Call clearinput()
            Else
                MessageBox.Show("invalid input")
                'Exit Sub
            End If

        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Invisible Fence", MessageBoxButtons.OK, MessageBoxIcon.Stop)

        End Try

    End Sub

I appreciate any help..thank you

Member Avatar for Unhnd_Exception

Change ex.ToString to ex.Message to get the proper meaning.

You should probably validate the length text box before using it.

If Not IsNumeric(txtLength.Text) then
 'Its not a number and should be converted to one.
endif

Thank you for your response.

I understand that I can validate it by using an if statement, but the professor wants us to use a try/catch as a way to produce the error message i stated before. I might not be explaining myself correctly..
We are working with classes. I have the isRun method in another class called fenceRun. When they click on the add button it adds the length to the list. if there is nothing in the textbox or if they type a letters instead of numbers then it triggers the exception stating the exception I stated above..
The way i am doing it.. which was using ex.tostring or ex.message or even invalidcastexception is not producing the unhandled message or the buttons he wants us to produce.
Thanks again for you help

Member Avatar for Unhnd_Exception

Your going to have to display your own message that sais that.

The only way I know to get that message is if the app is installed and the error isn't caught. Then windows will display that message.

I guess you have to create a form that displays the message you want then add three buttons. Looks like you need to pass the ex.message to the error message form and if the details button is pressed then show the message. If continue is chosen then don't do anything. If exit is chosen then Application.Exit() should be called.

Hope that helps.

I understand making a new class that displays 3 buttons...But How would I pass ex.message to the error message form whenever they click on add in the main form. And is application exit mean me.close or form name like main.close...not sure how to go about handling this..
thank you

Pass the ex.Message as a Parameter.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim x As String = ""
        Try
            x = CStr(CInt(x) + 1)
        Catch ex As Exception
            getErrorMessage(ex.Message)
        End Try
    End Sub
    Private Sub getErrorMessage(ByVal theReasonForTheError As String)
        MsgBox("Your error message is: " & vbNewLine & theReasonForTheError, MsgBoxStyle.Critical)
    End Sub
End Class

Application.Exit will close the entire application.
Me.Close will close the current Form.
Depending if set for "When last form closes" or "When startup form closes" in your project's Properties/Application tab, it will respond differently for Me.Close .

commented: thank you this worked great.. +1

After I did a separate form for the error message, it so happen that the professor had given me the running application outside of the code...since it did not know where to look for the unhandled error in a code, it automatically gave that error message..

PS..just in case it happens to someone else..
Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.