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