Java learning summary: 19

throws keyword

The throws keyword is mainly used in the method definition, indicating that this method does not handle exceptions, but is handed over to the called party for processing.

Example: use throws

package com.study.Demo;

class MyMath{
    public static int div(int x,int y)throws Exception{	//此方法不处理异常
        return x/y;
    }
}
public class Test4 {
    public static void main(String args[]){
        try{	//div()方法抛出异常,必须明确进行异常处理
            System.out.println(MyMath.div(10,5));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
//结果
//2

Tip: Throws can also be used on the main method.

Example: Use throws on the main method

package com.study.Demo;

class MyMath{
    public static int div(int x,int y)throws Exception{	//此方法不处理异常
        return x/y;
    }
}
public class Test4 {
    public static void main(String args[]) throws Exception{
    //表示此异常产生后会直接通过主方法抛出,代码中可以不强制使用异常处理
        System.out.println(MyMath.div(10,0));
    }
}
//结果
//Exception in thread "main" java.lang.ArithmeticException: / by zero
//	at com.study.Demo.MyMath.div(Test4.java:5)
//	at com.study.Demo.Test4.main(Test4.java:10)

throw keyword

The throw keyword can manually throw an instantiated object by the user.

Example: Manually throw an exception

package com.study.Demo;

public class Test4 {
    public static void main(String args[]){
        try{
            throw new Exception("自己定义的异常");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
//结果
//java.lang.Exception: 自己定义的异常
//	at com.study.Demo.Test4.main(Test4.java:11)

Standard format for exception handling

Example:

package com.study.Demo;

class MyMath{
    public static int div(int x,int y)throws Exception{	//出现异常要交给被调用处输出
        System.out.println("计算开始");
        int result=0;
        try{
            result=x/y;
        }catch (Exception e){
            throw e;	//向上抛
        }finally {
            System.out.println("计算结束");
        }
        return result;
    }
}
public class Test3 {
    public static void main(String args[]){
        try {
            System.out.println(MyMath.div(10,0));	//被调用处处理异常
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

RuntimeException类

The characteristics of the RuntimeException class: the
program will not force the user to handle the exception at compile time. The user can selectively handle it according to his own needs, but if an exception occurs without processing, the JVM will handle it by default.

Example:

package com.study.Demo;

public class Test4 {
    public static void main(String args[]){
        int temp=Integer.parseInt("100");
        System.out.println(temp);
    }
}
//结果
//100

This code runs normally, but if an exception occurs, it is handed over to the JVM for default processing.

assert keyword

The main function of the assert keyword is to make assertions. Assertion refers to the execution of a program to a certain line, the result must be the expected result.
Example: Observe the use of assertions

package com.study.Demo;

public class Test5 {
    public static void main(String args[]){
        int num=10;
        //假设中间可能经过了20行代码来操作num的内容,期望的内容应该是20
        assert num==20:"num的内容不是20";	//进行断言操作
        System.out.println("num="+num);
    }
}
//结果
//num=10

The result is no exception, this is because Java does not enable assertion by default. If you want to enable assertions, you need to do the following:

1. Open the corresponding control panel
Insert picture description here
2. Enter -ea
Insert picture description here

Custom exception

Exception classes developed by users.
If you want to implement a custom exception class, you only need to inherit the Exception or RuntimeException parent class.
Example: Define AddException

package com.study.Demo;

class AddException extends Exception{	//此异常类要强制处理
    public AddException(String msg){
        super(msg);	//调用父类构造
    }
}
public class Test5 {
    public static void main(String args[]){
        int num=20;
        try{
            if(num>10){	//出现了错误,应该产生异常
                throw new AddException("数值传递的过大");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
//结果
//com.study.Demo.AddException: 数值传递的过大
//	at com.study.Demo.Test5.main(Test5.java:13)
49 original articles published · Liked 25 · Visits 1528

Guess you like

Origin blog.csdn.net/weixin_45784666/article/details/104414321