String conversion numeric type exception analysis

Source: Internet
Author: User

In this article, let's examine the C # function int. Parse (), the exception that occurs when a string converts a numeric type.

Int. Parse (String str): This method converts a string of numeric content to an int type.

If the contents of the string are null, the ArgumentNullException exception is thrown;

Throws a FormatException exception if the string content is not a number.

Use this method to handle only the contents of a string, and the converted string content is within the range of the int type that can be represented.

OK, now for a test program:

This is the test interface.

Enter Int32 to indicate the number in the range:

Enter a non-numeric string:

Output the type of exception that occurred

Enter a number that is outside the range int32:

(This also knows that the maximum value represented by Int32 is 2147483647)

What happens if you don't type:

An empty string is not entered, and an exception with the same type as a non-numeric string occurs.

Here is the source code for the test program:

Try            {                int res = Int. Parse (TextBox1.Text);                Label3. Text = res + "";            }            catch (Exception Exception)            {                label3. Text = exception. ToString ();            }

Next, we will discuss the next convert.toint32, Int. Parse (int32.parse), Int. The difference between TryParse.

Convert.ToInt32 and Int. The Parse is more similar, in fact Convert.ToInt32 internally called Int. Parse:









Int. TryParse and Int. Parse is more similar, but it does not produce an exception, the conversion succeeds and returns True, and the conversion fails to return false. The last parameter is the output value, and if the conversion fails, the output value is 0.

String conversion numeric type exception analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.