Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Try-Catch Solution/ Debug Mini Calculator

Posted on 2004-04-23
Medium Priority
Last Modified: 2008-02-01
I have an application that is set up like a Mini calculator with Input Box 1 and Input Box 2 and oupt Put box for the answers. It contains radio button of Addition, subtraction, multiplication and division. The Variables FirstNum and SeconNum are declared as short and must remain as such.  (FistNum = TextBox1.Text and SecondNum = TextBox2.Text)

I have overflow and exception errors also division by zero errors. How do I write the Try...Catch statements to handle these errors and wher do they go in VB.Net
Question by:bobtman
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

Expert Comment

ID: 10905305
you should enclose the sentence that is originating the error with try and catch
I assume that those are in the click event of the button used to do the actual calculation.
can you post that code (the click event of the button)?
LVL 22

Expert Comment

ID: 10905351
try {
     do the calculation
catch (Exception e)
         // presumably you'll issue a message based upon which exception you got, then set the focus off somewhere and
         // exit this routine so you don't fall through to the good code below
// Code to run if the calculation was good

Accepted Solution

games0802 earned 2000 total points
ID: 10906347
In VB.NET, I believe what you need is simply the keywords Try and Catch.


'insert code that generates errors here
Catch e as Exception  'generally this would be more specific
Console.WriteLine("Error: " & e.Message)  'here is where you decide what to do with the error
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!


Expert Comment

ID: 10906352

Put your code within the try cath block as shown below:

    Dim FirstNum As Short
    Dim SecondNum As Short


      FirstNum = TextBox1.Text
      SecondNum = TextBox2.Text

      TextBox3.Text = FirstNum + SecondNum

    Catch ex As Exception
    End Try


Expert Comment

ID: 10908606
I would use functions depending on what option button you choose use that function:

TextBox3.Text = AddNumbers(TextBox1.Text, TextBox2.Text)
TextBox3.Text = DivideNumbers(TextBox1.Text, TextBox2.Text)

Private Function AddNumbers(ByVal Number1 As String, ByVal Number2 As String) As String
          AddNumbers = (CShort(Number1) + CShort(Number2)).ToString()
      Catch ex As Exception
          Return ex.Message.ToString()
      End Try
Return AddNumbers
End Function

Private Function DivideNumbers(ByVal Number1 As String, ByVal Number2 As String) As String
          DivideNumbers = (CShort(Number1) / CShort(Number2)).ToString()
      Catch ex As Exception
          Return ex.Message.ToString()
      End Try
      Return DivideNumbers
End Function

Take Care,


Expert Comment

ID: 12213443
I have a question. Having tried multiple samples of code from this site, I found that with my existing code, none of this seems to work. I am still filling in different parts of my program, but the parts that appear complete, don't work at all. If nothing else, I would really like to get the addition part right. HELP!

This is my code:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton3 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton4 As System.Windows.Forms.RadioButton
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.RadioButton4 = New System.Windows.Forms.RadioButton
        Me.RadioButton1 = New System.Windows.Forms.RadioButton
        Me.RadioButton3 = New System.Windows.Forms.RadioButton
        Me.RadioButton2 = New System.Windows.Forms.RadioButton
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.TextBox3 = New System.Windows.Forms.TextBox
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(16, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(88, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Variable 1"
        Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.Location = New System.Drawing.Point(16, 72)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(88, 16)
        Me.Label2.TabIndex = 2
        Me.Label2.Text = "Variable 2"
        Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label3.Location = New System.Drawing.Point(264, 16)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(96, 16)
        Me.Label3.TabIndex = 5
        Me.Label3.Text = "Result"
        Me.RadioButton4.Location = New System.Drawing.Point(8, 72)
        Me.RadioButton4.Name = "RadioButton4"
        Me.RadioButton4.Size = New System.Drawing.Size(88, 16)
        Me.RadioButton4.TabIndex = 3
        Me.RadioButton4.Text = "Division (/)"
        Me.RadioButton1.Location = New System.Drawing.Point(8, 24)
        Me.RadioButton1.Name = "RadioButton1"
        Me.RadioButton1.Size = New System.Drawing.Size(96, 16)
        Me.RadioButton1.TabIndex = 0
        Me.RadioButton1.Text = "Addition (+)"
        Me.RadioButton3.Location = New System.Drawing.Point(8, 56)
        Me.RadioButton3.Name = "RadioButton3"
        Me.RadioButton3.Size = New System.Drawing.Size(104, 16)
        Me.RadioButton3.TabIndex = 2
        Me.RadioButton3.Text = "Multiplication *)"
        Me.RadioButton2.Location = New System.Drawing.Point(8, 40)
        Me.RadioButton2.Name = "RadioButton2"
        Me.RadioButton2.Size = New System.Drawing.Size(104, 16)
        Me.RadioButton2.TabIndex = 1
        Me.RadioButton2.Text = "Subtraction (-)"
        Me.Button1.Location = New System.Drawing.Point(280, 72)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(72, 24)
        Me.Button1.TabIndex = 7
        Me.Button1.Text = "Calculate"
        Me.Button2.Location = New System.Drawing.Point(280, 112)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(72, 24)
        Me.Button2.TabIndex = 8
        Me.Button2.Text = "Quit"
        Me.GroupBox1.Location = New System.Drawing.Point(120, 24)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(128, 104)
        Me.GroupBox1.TabIndex = 4
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Operator"
        Me.TextBox2.Location = New System.Drawing.Point(16, 88)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(88, 20)
        Me.TextBox2.TabIndex = 3
        Me.TextBox2.Text = ""
        Me.TextBox3.Location = New System.Drawing.Point(264, 32)
        Me.TextBox3.Name = "TextBox3"
        Me.TextBox3.Size = New System.Drawing.Size(112, 20)
        Me.TextBox3.TabIndex = 6
        Me.TextBox3.Text = ""
        Me.TextBox1.Location = New System.Drawing.Point(16, 32)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(88, 20)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = ""
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(392, 165)
        Me.Name = "Form1"
        Me.Text = "Basic Math Tests"

    End Sub

#End Region

    'Declare FirstNum and SecondNum variables
    Public FirstNum, SecondNum As Short
    Public ExceptionFlag As Short

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Assign text box values to variables

        FirstNum = TextBox1.Text
        SecondNum = TextBox2.Text

    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged

        ' Calculate Using Button 1 and catch exceptions (Addition)
            TextBox3.Text = FirstNum + SecondNum
        Catch Exception As System.OverflowException
            MessageBox.Show("This number is TOO LARGE. Please clear your input and try again" & Exception.Message)
        End Try

    End Sub
    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
            TextBox3.Text = FirstNum - SecondNum
        Catch Exception As ()
        End Try
    End Sub

    Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
            TextBox3.Text = FirstNum * SecondNum
        Catch Exception As ()
        End Try
    End Sub

    Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
            TextBox3.Text = FirstNum / SecondNum

        Catch Exception As DivideByZeroException

            MessageBox.Show("Cannot devide by zero!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

Expert Comment

ID: 12213527
Would you please let me know of a way to fix the addition part without ruining the rest of the calculations?

Expert Comment

ID: 12213664
Please disregard my message about the addition. I have modified my code to fix that. I would like some help with the division are though. The calculator program does not bomb-out as a result of a divide by zero condition. How could I test for an exception, when no exception appears to occur. Please refer to my submitted code. Thanks.

Featured Post

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.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans

597 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