Article 8 exception of chapters

Source: Internet
Author: User

A well-defined API that should include methods for using pre-and post-conditions.

However, the operating environment is complex. The program can encounter various errors during the run.

To do this, do some preparation. when programming, anticipate that the method may run into unexpected situations. The source of the error, which may be an inappropriate external environment, may be that the method caller has passed an inappropriate implementation-that is, the wrong environment or an illegal argument.

The designer of the service must take into account the possible exception events, must consider throwing exception objects, and make the documentation for this method a good interface for the method .

The designer of the method is obliged to Check the validity of the method parameters。 The check for illegal parameters is the first step in the method implementation, regardless of whether the parameters in the method's document are used in terms of preconditions to be clearly explained.

If you use the traditional error-handling mechanism (like the C language) without using an exception object, CharAt () will have to return a special value for the exception event. The caller infers the return value to handle the exception, read [routine 8-4 code clutter] Today (the routines are placed in [8.1.4 's own definition exception class] in order to make it easier to compare traditional means and define exceptions yourself). There are many problems with this traditional approach:

    • Code promiscuous.

      The normal function code is mixed with a lot of code that handles the various errors that can occur in the system. Significantly reduces the readability of the program.

      Separating the business logic from exception handling can be said to be the most important purpose of the Java anomaly mechanism.

    • The same error is not abstracted by data.

      Encapsulates an exception or an error with some type of data. Instead of distributing the code with the same error in every corner of the program, you can effectively enhance reusability. The construction of the standard Exception class library makes the program ape no longer needs to devote too much effort on the problem of exception handling.

    • A special return value problem.

So the traditional language such as C, handling anomalies is more troublesome. Some people take advantage of powerful setjmp and LONGJMP functions, and apply macros extensively to achieve the syntactic effects of exception handling in C + + and Java.

Exercise 8-3: Demonstrate the advantages of an object-oriented exception handling mechanism.


When program execution encounters exception, does the program end execution or should it recover from an exception event ? It depends on the severity of the exception.

(1) Terminally ill Let the program euthanasia, (2) Big disease small disaster can heal. (3) Program bugs (abnormal events that should not occur) are ignored (call to write the program that led to the exception code ape, let him change).

The java.lang.Throwable class is the parent class for all errors or exceptions in the Java language. The case (1) is that a serious system error is encapsulated as error, and the case (3) is that the exception that needs to be removed is encapsulated as runtimeexception.

Both cases do not need to deal with the exception, but to look for logic in the source code or use method errors and changes. The case (2) is the exception that should be handled . They and the case (3) are exception subclasses, so exception and its non-runtimeexception subclasses, the compiler feels that the caller of the method must handle such exceptions. It also supervises, checks whether the caller has handled the exception, and therefore calls them the checked exception (checked Exceptions). Commonly referred to as check-type exceptions, spoken as " exceptions to be handled ."

So:

    • Error: The program ape does not throw, do not capture, do not handle.

    • RuntimeException: By the system through the default exception handler self-throwing, self-processing. It is not obligatory to require the program ape to catch and handle.

      Correct when debugging the program.

    • Check-type exception : The method header is declared with a throws clause, the method body is thrown with a throw statement, the caller throws clause up to handle responsibility, or use try-catch capture and processing.


There are a number of examples in JUnit that can be used as supplementary materials for learning Java anomalies.

Added in the relevant section.


watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvexfqmja2nq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "style=" border:none; max-width:100% ">


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Article 8 exception of chapters

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.