Int. Parse Exception Analysis report

Source: Internet
Author: User

In this blog, mainly around Int. The exception for parse is described.

use int in C #. When parse converts other types to int, the running program often throws the following three types of exceptions:

1,system.argumentnullexception Convert object to NULL
2,system.formatexception format is incorrect
3,system.overflowexception Conversion Object Overflow

For system.argumentnullexception this case, int.parse when judging that the converted object is null, it cannot convert a fixed value, if it is convent.toint32 () to convert null then return the poem 0, actually Convent.toint32 () and Int. Parse is similar because the former actually invokes the latter for processing internally, whereas Convent.toint32 () can convert more types, and the exception handling mechanism is better than int. Parse (). But since similarities also have the same place, both will throw an exception when both of the objects are "".

System.FormatException, which indicates that the type of the converted object does not match the target type, such as int nbalance = Int. Parse (strbalance); strbalance=100.00. Strbalance is not an integral type after conversion, it throws a System.FormatException exception at a time. Modify int nbalance = Int. Parse (strbalance) is decimal nbalance = decimal. Parse (strbalance); At this point the transformation can proceed without throwing an exception. In fact here Convent.toint32 () also powerless, although it can be converted to more object types, but in the conversion of the object format is not correct here, it also can not make any effective measures in addition to error accident.

Overflow is an enduring problem throughout the computer industry, where System.OverflowException is an overflow of converted objects, possibly overflow, or overflow, when the object overflows, int. The parse function does not make any other behavior, such as when an overflow occurs, the value of the overflow boundary is taken as the conversion object, or as a limit. Int. Parse throws an exception directly when it overflows with the overflow.

When converting other types to int, there is actually an int. Tryparse,int. TryParse and Int.parse are similar, but Int.tryparse does not produce an exception because Int.tryparse (bool,int) is a function that can return a bool object, but when the conversion succeeds, returns True, and the conversion fails to return false. The last parameter is the output value, and when the conversion fails, its value is 0.

use int in C #. The parse function should try to avoid the occurrence of throwing exceptions, and if not avoided, choose a different conversion function, such as Convent.toint32 (), Int. TryParse (Bool,int) and so on.

Int. Parse Exception Analysis report

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.