Chapter 2 exceptions

Source: Internet
Author: User

A well-defined API should contain the pre-conditions and post-conditions for using this method. However, the running environment is complex,The program may encounter various errors during execution.. For this reason, the source code should be prepared for possible errors. It should be foreseen during programming that unexpected conditions may occur during method execution. The root cause of the error may be an inappropriate external environment. It may be that the method caller has passed an inappropriate real parameter-that isIncorrect EnvironmentOrInvalid Parameter.

The service designer mustTake into account possible exception events, must consider throwing exception objects, do a good job of this method documentation to make the method has a good interface.

The method designer is obligated Check the validity of method parameters. Whether or not the pre-conditions are used in the method documentation to explicitly describe the parameters, checking invalid parameters is the first step to implement the method.

If the traditional error handling mechanism (such as the C language) is used without exception objects, charat () must return a special value for the exception event. The caller judges the returned value to handle the exception, please read [routines 8-4 mixed Code] (routines are placed in [8.1.4 custom exception classes] to facilitate the comparison of traditional methods and custom exceptions ). This traditional method has many problems:

  • Mixed code. The normal functional code is mixed with a large number of codes that process various errors in the system, seriously reducing the readability of the program. Separating business logic from exception handling is the most important purpose of Java exception mechanism.
  • The same error is not abstracted. Encapsulate exceptions or errors in a certain data type, instead of distributing codes that detect the same error in every corner of the program, effectively enhancing reusability. The construction of standard exception class libraries frees programmers from spending too much effort on Exception Handling issues.
  • Special return value issues.

Therefore, it is troublesome to handle exceptions in traditional languages such as C. Some people use powerful setjmp and longjmp functions and apply Macros in large quantities to achieve the syntax Effect of exception handling in C ++ and Java.

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


When the program execution encounters an exception, the program isThe running should be recovered from the exception event.What about it? This depends on the severity of the exception.

(1) Let the program be put down after illness, (2) major illness and minor disaster recovery can save lives, (3) program bugs (abnormal events should not occur) then ignore it (call the programmer who compiled the code that caused the exception and ask him to modify it ).Java. Lang. throwableClass is the parent class of all errors or exceptions in Java. Case (1) that is, serious system errors are encapsulatedError, Condition (3) that is, the exception to be removed is encapsulatedRuntimeexception. In both cases, you do not need to handle the exception event. Instead, you need to find logic or usage errors in the source code and modify them.Case (2) is the exception to be handled, And Case (3) are both subclasses of exception, so exception and its non-runtimeexception subclasses, the compiler considers that method Callers must handle such exceptions, it also monitors and checks whether the caller has handled the exception.The exception to be checked (Checked exceptions)It is generally called a checktype exception and is colloquial as"Exception to be handled".

Therefore:

  • Error: the programmer does not throw, capture, or handle the error.
  • Runtimeexception: the system automatically throws a runtimeexception through the default exception handler. It is not mandatory for programmers to capture and process. Correct the code when debugging the program.
  • Check type exception: Use the throws clause in the method header and throw the throw statement in the method body. The caller uses the throws clause to shirk the processing responsibility or use try-catch to capture and process the request.


JUnitThere are many examples that can be used as a supplement to learning Java exceptions. Add it in the relevant section.


Chapter 2 exceptions

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.