use try...catch alone to input numbers less than 19 digits long in text box

Posted on 2006-07-01
Last Modified: 2010-04-17
Using try..catch statement alone i am trying to input a number in a text box. I am trying to make it so that a message pops if the input is 19 9s or more. I dont want the answers textbox to display infinity, instead i want it to pop a msgbox saying the input is too big. Here is the code: (please try not to use if...else statements, only try...catch)

 'defines what happens when the user clicks on the calculate button. It displays the equation on the text box for result.
    Private Sub CALCULATEButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CALCULATEButton.Click


            'assigns the value of the RADIUS text box to the radius variable
            radius = RADIUSTextBox.Text

            'Determine what radio buttons are checked and calculate according
            If SURFACERadioButton.Checked = True Then
                equation = 4 * PI * (radius ^ 2)
            End If

            If VOLUMERadioButton.Checked = True Then
                equation = (4 / 3) * PI * (radius ^ 3)
            End If

            RESULT = CStr(equation)

            'displays the result of the equation
            RESULTTextBox.Text = equation

        Catch ex As OverflowException
            Console.WriteLine("Error: Input too large/small, Calculated value too large/small")
            MsgBox("Error: Input too large/small, Calculated value too large/small" & vbCrLf & "Please enter a different value", , "Sphere Volume and Area Calculator Version 2")
            RADIUSTextBox.Text = 1

        Catch ex As InvalidCastException
            Console.WriteLine("Error: incorrect input, string used")
            MsgBox("Please enter a valid number!" & vbCrLf & "Expressions like a , ! @ # $ % ^ & * ( ) _ + = | / \ { } [ ] ' : ; < > ?" & vbCrLf & "are not accepted.", , "Sphere Volume and Area Calculator Version 2")
            RADIUSTextBox.Text = 1

        End Try

    End Sub

Question by:supercoqui
  • 5
  • 3

Expert Comment

ID: 17025014
use a checked statement

for example

                checked { Convert.ToInt32(RADIUSTextBox.Text);  }
            catch (OverflowException e)
                Console.WriteLine("Error caught: {0}", e);


Expert Comment

ID: 17025021
btw, this is in C#, should be easy to convert to

Author Comment

ID: 17025033
wow, almost works, it blocks on 10 9s and there after, but i can still get values with 18 9s without using the statement provided. My normal code gives the infinity at 19 9s, so i am trying for it to work normally if i input 18 9s.

I only used this part because the rest gave me error:

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.


Author Comment

ID: 17025047
ok I changed the value to
and it works fine for the surface, on 19 9s it catched the exception.

Now the problem lies when i do it for volume, volume only works with 12 9s and then it goes into displaying infinity.

Accepted Solution

bramsquad earned 500 total points
ID: 17025080
the biggest number a computer can handle is an unsigned long int....and it depends on how many processor bits it is

if you do Convert.ToUInt64(RADIUSTextBox.Text) that will kick back all values more than


you could do a Convert.ToInt64(RADIUSTextBox.Text) - this will error values larger than


which is slightly less than 19 9's and more than 18 9's may emulate a 64 bit processor, but if you only have a 32 bit cpu you may get an error.  i dont know

other than that the only way is to do a string conversion



Author Comment

ID: 17025102
I found the way messing around with the information provided by bramsquad, thanks

Expert Comment

ID: 17025107
hmm, 12 9's is weird because even an UInt32 is less than 10 9's

are you doing the same checked statement?  something like

checked { Convert.ToInt64(VOLUMETextBox.Text);  }


Expert Comment

ID: 17025110
well done

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 144
wordlen challenge 3 66
Query Syntax 17 34
what is the best portable software where i can test codes written in different languages 1 28
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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 …
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…

776 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