Basic concepts and process flow of an abnormality java

First, the basic concept of abnormal

Java in the case leading to interrupt the operation of the program is divided into two, one is an exception, and another called the error. Is the base class for all exceptions Exception, error base class is Error. Exception is controlled in java program code, programmable, and Error refers Java runtime system is displayed with the runtime errors related to the system itself, Error disastrous for the programmer, can not manipulate such jvm stack overflow error, the operating system memory allocation errors. Whether Exception or Error, their common parent class is java.lang.Throwable (be thrown) class.

Abnormality is an abnormal condition in a program run possible, these circumstances will cause an error, this error is different from coding errors or procedural error algorithm, coding errors can not compile, and the program will get the error calculation algorithm error of result. When an exception occurs the program, the default will be a direct result of the program interrupt the operation, abnormal early to predict this possibility can add logic exception handling, from this perspective, the exception is an effective mechanism to deal with logic, in the program or take the initiative to declare the exception thrown is also very common.

In short, in order to improve the robustness of the program, an exception needs to be processed by the programmer.

Second, the function and the abnormal process flow

1, when you need to catch the exception

Let's take a look at the following code:

public class Cal {
    public int div(int a, int b) {
        int result = a / b;
        return result;
    }
    public static void main(String[] args) {
        Cal cal=new Cal();
        int result=cal.div(10, 2);
        System.out.println("结果:"+result);
    }
}

 

Here we designed Cal div class and its methods for calculating the quotient of two numbers, in the main method we call this method, normal output: Result: 5; but if the user calls with the following code (note : the divisor is 0.)

public static void main(String[] args) {
        Cal cal=new Cal();
        int result=cal.div(10, 0);
        System.out.println("结果:"+result);
    }

 

Grammar of the program is no problem, but the program does not have to show normal results, the console output is as follows:

Program because of abnormal termination of the program and cause no output, this should be an exception captured by the programmer display to adjust the program robustness.

2, the process flow description and keywords

Keywords exception handling mainly used: try, catch, finaly, throw, throws. Its function is as follows:

 FIG try seen from the above, catch, the finally often used in combination, the finally is optional, but must be used with the try and catch. Its flow of execution is shown below:

Specific syntax is:

try{

The code may appear abnormal

} Catch (Exception class object) {

Treatment of abnormal, if not unusual, this is not to enter.

}finally{

Whether or not an exception will be in the final execution of the code.

}

Then modify the above main method. Look no output abnormal time. code show as below:

public static void main(String[] args) {
        Cal cal=new Cal();
        try{
            int result=cal.div(10, 2);
            System.out.println("结果:"+result);
        }catch (Exception e) {
            System.out.println("发生异常了。");
        }finally{
            System.out.println("结束。");
        }
        
}

 

程序并没有发生异常,所以catch的后面的语句块没有进入了,控制台输出如下图:

修改一下main方法,使除数为0。

public static void main(String[] args) {
        Cal cal=new Cal();
        try{
            int result=cal.div(10, 0);//这一句发生了异常,后面的代码就不会
            System.out.println("结果:"+result);//这句没有执行
        }catch (Exception e) {
            System.out.println("发生异常了。");//进入了异常处理
        }finally{
            System.out.println("结束。");//这句还是会执行
        }
        
}

 

Guess you like

Origin www.cnblogs.com/weibanggang/p/11184691.html