Java Code of violations

Source: Internet
Author: User

In Java, for client programmers who want to invoke methods, we want to inform them that they may "throw" the offending in their own way. This is a polite way to make it possible for the client programmer to know exactly what code to write to capture all potential violations. Of course, if you also provide the source code, the client programmer can even check the codes to find the appropriate throw statements. However, it is not usually accompanied by a source repository. To solve this problem, Java provides a special syntax format (and forces us to adopt) to politely tell the client programmer that the method will "throw" the offending and make it easy for the other person to control. That's what we're going to talk about here, "the offending code," which is part of the method declaration, which is behind the argument list.
The offending specification uses an additional keyword: throws followed by all potential types of violations. Therefore, our method definition should look something like the following:
void F () throws Toobig, Toosmall, Divzero {//...
If you use the following code:
void f () [///...]
It means that the violation is not "tossed" from the method (except for the type of runtimeexception, which may be thrown from anywhere-and later in detail).
But it is not entirely dependent on the violation-if the method causes an offence but does not control it, the compiler detects the situation and tells us that it is necessary to control the violation, or that an offending rule should be "thrown" from the method. By insisting on arranging the offending specification from top to bottom, Java can guarantee the correctness of the violation at compile time (note ②).

②: This is a notable improvement on the basis of C + + violation control, which does not capture errors that do not conform to the offending specification unless it is operational. This makes C + + 's illegal control mechanism appear to be of little use.

We can resort to deception in this place: "Throw" a violation that has not occurred. The compiler understands our requirements and forces users of this method to assume that the offending process is actually generated. In practical applications, it can be used as a "placeholder" for that violation. In this way, the actual violation can be easily generated later, without the need to modify the existing code.

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.