Home  >  Article  >  Java  >  What is the way to define custom exceptions in Java?

What is the way to define custom exceptions in Java?

WBOY
WBOYforward
2023-04-22 16:52:08655browse

1. What types of exceptions are there?

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

Compile-time exceptions

  • are all Exception classes and their subclasses

  • must be handled explicitly , otherwise an error will occur in the program and cannot be compiled

##Runtime exceptions

  • are all RuntimeException classes and their Subclasses

  • No need to display the processing, and can also be processed 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 class

Create a new test class below, 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.

The above is the detailed content of What is the way to define custom exceptions in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete