Java异常体系图

异常处理

异常体系图
在这里插入图片描述

异常分量大类

1. 编译异常(需程序员自己处理)
2. 运行异常

五大运行时异常
1. NumberformatException
2. ClassCastException
3. ArrayIndexOutOfBoundsException
4. ArthmeticException
5. NullPointerException

常见写法

    // #1
    try {
        
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        
    }
    
    // #2
    try {
        
    } catch (ArithmeticException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace(); // ...catch
    } finally {
        
    }
    
    // #3
    try {
        
    } catch () {
        
    }
    
    // #4
    try {
            
    } finally {
        
    }
throws 将异常抛出,交给上层处理

异常不处理也可以抛出

void method() throws FileNotFoundException, ArithmeticException, StackOverflowError, ArrayIndexOutOfBoundsException, ClassCastException {

}

方法被重写时,子类异常要是父类异常的子类或者和父类异常一样

throws 和 throw 的区别, throws是异常处理的一种方法,throw是抛出生成的异常对象

一些demo

package edu.chen._exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * @author Rw-chen
 * @version 0.1
 */

class FileTest {
    public static void test() throws Exception {
        FileInputStream fs = new FileInputStream("D:\\8069ac814fc0d5c9152ec8e871e70735\\目录说明.txt");
        System.out.println(fs);
    }

}

class MyException extends RuntimeException {
    public MyException(String message) {
        super(message);
    }
}

public class Main {
    static Scanner sc = new Scanner(System.in);
    static int intputInteger(int age) throws MyException {
        if (!(age >= 18 && age <= 120)) throw new MyException("年龄在18到120间");
        int ret;
        System.out.println("请输入一个整数");
        while (true) {
            try {
                ret = Integer.parseInt(sc.next());
            } catch (Exception e) {
                System.out.println("输入的不合法,请重新输入");
                continue;
            }
            return ret;
        }
    }

    void method() throws FileNotFoundException, ArithmeticException, StackOverflowError, ArrayIndexOutOfBoundsException, ClassCastException {

    }

    public static void main(String[] args) throws Exception {
        FileTest.test(20);
        System.out.println(String.class);
        System.out.println("abc".getClass().getName());


        System.out.println(intputInteger(0));

        int x = 10;
        try {
//            int a  = 10 / 0;
            String s = null;
            s.getBytes();
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (ArithmeticException e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值