The understanding of Java exception handling __java

Source: Internet
Author: User
Tags exception handling function definition

1. Differences between anomalies and traditional processing mechanisms

The Java exception mechanism has the following advantages over the traditional return code in the form of an upgrade that returns an error via a return value:

1. Abnormal carry more information, easy to analyze, such as carrying the call location information, error details

2. Can save a lot of code

For example, the traditional program to execute a SQL, you need to determine whether the results are correct, how many SQL may have how many judgments, and the exception mechanism, only to intercept a processing outside the database exception can be

3. Can force request judgment, traditional procedure, function return-1,-2,-3 but not necessarily the programmer remembers to judge, but the exception throw SQLException throw Xxxexception, throws how many kinds, outside needs to handle how many kinds of

4. Traditional return 1,2,3,4 are difficult to memorize, and exceptions are similar to enumerations, making it easy for programmers to remember and use

5. The exception can be a layer of not processing, directly thrown to the top, and the return value must be a layer of judgment, and then return, trouble


2. My summary of how to use exceptions in projects

1. In the project such as Java EE, the project program layered processing, the various levels of errors, need to log and as required in the user page display return, therefore, through the exception of the processing to achieve error flow control

2. If in our program, directly using the underlying exception, such as a function throws SQLException, then, if the next layer to add additional processing, you need to throw other similar to the SQLException exception, is bound, the function definition needs to be modified, so, All the places used in this function involve the interception of new exceptions or continue to be thrown, and such modifications are costly

3. Because of the above reasons, in the project, I think it is best to establish their own exception class, inherit from the exception, the functions within the project, if necessary, throw myexception, and the function of the internal need to intercept a variety of anomalies translated into myexcption, you can directly use the constructor, Construct various exceptions as Myexption

4. At the top, that is, struts action, intercept myexption, as needed to return to the display or log, etc.

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.