Error 错误和Exception异常 自定义异常

Error 错误和Exception异常 自定义异常

alt + enter处理异常

ArrayIndexOutOfBoundsExceotion(数组下标越界)

NullpointerException(空指针异常)

ArithmeticException(算数异常)

MissingResourceException(丢失资源)

ClassNotFoundException(找不到类)

异常五个关键字

try finally catch throw throws

package Java学习.chen.haha.ac.dome7;
//假色需要捕获多个异常需要从小到大   具体异常   Exception  Error  Throwable
//ctrl + alt  +t
public class dome1 {
    public static void main(String[] args) {
        int a = 2;
        int b = 0;
        try {//监控区域
            if(b==0){//增加代码块处理异常
                System.out.println(1);
                throw new ArithmeticException();//主动抛出异常
            }
            System.out.println(a / b);//Exception in thread "main" java.lang.ArithmeticException: / by zero
        }catch (ArithmeticException n){
            System.out.println("程序抛出异常");
        }finally{
            System.out.println("finally");
        }


    }
}
package Java学习.chen.haha.ac.dome7;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.w3c.dom.ls.LSOutput;

public class dome2 {
    public static void main(String[] args) {
        int a = 2;
        int b = 0;
        try {
            System.out.println("a/b");
        } catch (Exception e) {
            System.out.println("出现异常");

        }
    }
}
package Java学习.chen.haha.ac.dome7;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.w3c.dom.ls.LSOutput;

public class dome2 {
    public static void main(String[] args) {
        new dome2().test(1,0);

    }
    //假设这方法中,处理不了这个异常 方法上抛出异常
    public void test(int a,int b){
        if(b==0){
            throw new ArithmeticException();
        }
        System.out.println(a/b);//方法体  一般在方法中使用
    }
}

image
image

posted @ 2021-05-06 10:23  陈诚成  阅读(191)  评论(0)    收藏  举报