Java exception/error mechanism

Source: Internet
Author: User

Java exception/error mechanism

Javar
There are two main types of exceptions: checked exceptions and non-checked exceptions.

How to use exceptions? For what purpose?

Check exceptions are generally used at the API layer. The client must consider exceptions when calling statements, if an exception occurs, the client program must handle the exception based on its own scenario (record, throw the exception to the upper layer, jump to another processing policy ).

What are the more detailed considerations? Policy for throwing an exception to the upper layer? What are the policies for redirecting to other branches?

Abnormal record policy?

  1. The purpose of recording exceptions is to quickly locate the problem.
  2. Duplicate records are not required.
  3. Records where exceptions are initially generated.
  4. A unified style is required, and a log processing system is considered.

Throw an exception policy?

An exception has a hierarchical structure. Simply throwing an exception does not make any sense. The key is

  1. What is the overall processing process like?
  2. What is the overall call process?

What kind of exceptions will occur during a specific process can be pre-agreed.

For example,

  1. Exceptions at the UI rendering layer are closely related to users, and the exception information is usually not stack output.
  2. Exceptions at the business logic layer are closely related to the business and developers. In addition, this layer indirectly affects the exception handling at the UI Layer, usually, this layer of exception information contains the business-level and technical-level mixed exception information. This layer of exception information contains the business Tag Information and part of the stack information.
  3. The underlying technical support layer is closely related to the provided API callers. The exception information of this layer is mainly stack information.

This can be agreed!

We define a set of software projects with rules to constrain such a layered structure, the UI Layer, logic layer, and technical support layer define the exception handling classes for each layer (the class granularity can be freely controlled but must have rules ), when it comes to the filtering and transmission of abnormal data between layers, we can use inheritance and Polymorphism to smoothly complete class conversion, what we need is the exchange of exception information, so we don't need to care about which class it is. The role of a class is limited to the constraints at the code structure level.

Related Article

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.