". NET programming Pioneer C #" seventh chapter exception handling (Turn)

Source: Internet
Author: User
Tags exception handling
Seventh Chapter exception Handling
One of the great advantages of the common language runtime (CLR) is that exception handling is standardized across languages. An exception that is thrown in C # can be handled in the Visual Basic client. There is no longer a hresults or ISupportErrorInfo interface.
Although the coverage of cross language exception handling is extensive, this chapter focuses entirely on C # exception handling. You slightly change the compiler's overflow behavior, and then the interesting thing begins: you handle the exception. To add more means, then raise the exception you created.
7.1 Checksum (checked) and non-checksum (unchecked) statements
When you perform an operation, it is possible that the calculated result is beyond the valid range of the result variable data type. This situation is called overflow, and depending on the programming language, you will be notified in some way-or not at all. (Does C + + programmers sound familiar?) )
So, how does C # handle overflow? To find out its default behavior, see the example of factorial I mentioned earlier in this book. (For your convenience, the previous example is shown again in Listing 7.1)
Listing 7.1 calculates the factorial of a number
1:using System;
2:
3:class factorial
4: {
5:public static void Main (string[] args)
6: {
7:long nfactorial = 1;
8:long Ncomputeto = Int64.parse (Args[0]);
9:
10:long ncurdig = 1;
11:for (Ncurdig=1;ncurdig <= ncomputeto; ncurdig++)
12:nfactorial *= Ncurdig;
13:
14:console.writeline ("{0}! is {1} ", Ncomputeto, nfactorial);
15:}
16:}
When you use the command line to execute a program like this
Factorial 2000
The result was 0 and nothing happened. Therefore, it is safe to assume that C # silently handles overflow situations without explicitly warning you.
You can change this behavior by allowing overflow checksums for the entire application (the compiler switches) or at the statement level. The following two sections address one scenario separately.
7.1.1 set compiler for overflow checksum
If you want to control overflow checksums for the entire application, the C # compiler settings selection is exactly what you're looking for. By default, overflow checksums are disabled. To explicitly ask for it, run the following compiler command:

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.