Java-Exception Explanation

Source: Internet
Author: User

(i) The reason for the occurrence of abnormal mechanism

Exception handling can enhance the robustness of the program, in C language is the function return value to handle the exception, the disadvantage of this practice:

1. The return value may conflict with existing logic.

2. Poor code readability. The execution code and exception handling code are mixed together.

3. You need to know the function return value details to do the processing.

/** * Exception Handling Demo *  * @author Peter_wang * @create-time 2014-9-24 morning 9:24:35 */public class Exceptiondemo {    /**     * @param args     */public    static void Main (string[] args) {        int num = operatenum (5);        The external user of the function needs to know that the return value        of the exception//function evaluates to exactly 1, is not inherently exception range        //exception handling aggravating program coupling, exception handling and normal handling are confused with        if (num = = 1) {            SYSTEM.OUT.PRINTLN ("Incoming parameter is incorrect");        }        else {            System.out.println ("Calculated result:" + num);        }    }    private static int operatenum (int num) {        if (num = = 0) {            return-1;        }        num = 10/num;        num-= 3;        return num;    }}


(ii) Classification of anomalies


System-Level errors (Error): If memory consumption is exhausted, there is usually no need to bother.

Compile-time error: This exception must be wrapped in the captured statement that is displayed in the code, otherwise compiled, such as FileNotFoundException.

Run-time error: Code is not spec-generated error, error requires programmer to change bug, such as null pointer exception and array subscript out of bounds.


(iii) Use of exceptions

1. Do not ignore exceptions

        try {           ...        }        catch (Exception e) {        }
do not handle the exception, contrary to the original intention of the design, the program error difficult to find the cause of the error.

2. Do not separate logically strong code

FileInputStream is = null;        try {is            = new FileInputStream (New File (""));        catch (FileNotFoundException e) {            e.printstacktrace ();        }        try {            is.read ();        }        catch (IOException e) {            e.printstacktrace ();        }
exception snapping separates logically strong code from poor readability. The following code should be used

FileInputStream is = null;        try {is            = new FileInputStream (New File (""));            Is.read ();        }        catch (FileNotFoundException e) {            e.printstacktrace ();        }        catch (IOException e) {            e.printstacktrace ();        }
3. Can handle the exception does not pass up

There are two ways to handle exceptions: Handle the catch yourself and pass the throw up.

The upward pass represents too much of an unknown, capable of handling itself, unless it is known beforehand that the information needs to be processed before being passed.

4.IO Exception Handling

If you are using resources such as a database connection or a network connection, remember to do some cleanup work in finally (such as shutting down the database connection or network connection).

5. Do not use exception to catch anomalies

Fine-grained exception types can facilitate processing and error message capture.



Java-Exception Explanation

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.