How to customize exceptions in Java?

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.

Article directory


Preface

        If there is a problem with the program, we do not do any processing. In the end, the JVM will do the default processing. The processing method has the following two steps: output the name of the exception, the cause of the error, the location of the exception and other information on the console, and stop the execution of the program. .

  • The program starts executing from the code inside try
  • If an exception occurs, it will jump to the corresponding catch for execution.
  • After execution is completed, the program can continue to execute

提示:以下是本篇文章正文内容,下面案例可供参考

1. What are the types of exceptions?

Exceptions are divided into two types, namely compilation exceptions and runtime exceptions .

        compile time exception

  • All are Exception class and its subclasses
  • The processing must be displayed, otherwise an error will occur in the program and it will fail to compile.

        runtime exception

  • They are all RuntimeException classes and their subclasses
  • No explicit processing is required, and it can be handled in the same way as compile-time exceptions.

        

package com.xxgc.chop5_2.test;

public class ExceptionDemo {
    public static void show4(){
        //把字符串转换int类型
        String a="张三";
        int b=Integer.parseInt(a);//NumberF
    }
    //异常抛出
    public static void show3() throws ClassNotFoundException {
        Class.forName("Student");
    }
    public static void show2(){
        //运行时异常:程序运行的时候出现的异常,可以try
        //编译时异常(非运行时异常):必须try catch 或者向上抛出
        try {
            Class.forName("Student");
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    public static void show() {
        //制造一个异常,捕获异常,处理异常
        try{
           int []nums={1,2};
           int n=10/0;
           int a=nums[3];
        }catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
            System.out.println("数组下标出错了");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("出错了");
        }finally {
            //最终最后都要之心的代码,一般完成资源释放工作
            System.out.println("最终的!!!");
        }
    }

    public static void main(String[] args) {
        //trows:向上抛出异常,抛给方法的调用者
        //show3()方法向上抛出了异常,需要main方法解决
        //1.main方法解决了
        //2.main没解决完,继续向上抛,jvm(Java虚拟机)解决
        try {
            show3();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

2. Custom exceptions

1. First create a new class

        This class is a custom exception class. First, we inherit the RuntimeException of idea, and then create methods with and without parameters.

The code is as follows (example):

package com.xxgc.ch06.po;

public class MyException extends RuntimeException{
    public MyException(){

    }
    public MyException(String s){
        super(s);
    }

}

2. Test category

        Next, create a new test class with main method and shou method. Define an int type a in the shou method, and enter if to determine whether a is abnormal.

The code is as follows (example):

package com.xxgc.ch06.test;

import com.xxgc.ch06.po.MyException;

public class ThrowDemo {
    public static void show(){
        //如果a>10,抛出自己的异常
        int a=13;
        if (a>10){
            try {
                throw new MyException("不能大于10");
            }catch (MyException e){
                e.printStackTrace();
                System.out.println("出错啦!"+e.getMessage());
            }


        }
        System.out.println("扶苏");
    }

    public static void main(String[] args) {
        show();
    }
}

The idea software used here.


Summarize

Here is a summary of the article:
The above is what I will talk about today. Java custom exceptions need to create a new class and write methods with parameters and no parameters. This type of exception needs to inherit the RuntimeException of idea. Next you need to test whether it will be displayed.

The following are the running results:

com.xxgc.ch06.po.MyException: cannot be greater than 10
    at com.xxgc.ch06.test.ThrowDemo.show(ThrowDemo.java:11)
    at com.xxgc.ch06.test.ThrowDemo.main(ThrowDemo.java:23 )
Something went wrong! Cannot be larger than 10
Fusu

Process ended with exit code 0

Guess you like

Origin blog.csdn.net/qq_68384595/article/details/127066148