Java异常处理机制

Throwable异常类:1.exception。2:error(虚拟机错误,程序员无能为力)

exception分为runtimeException(及其子类叫做UNCHECK exception,比如ArithmeticException)和checkException(IOException,IndexOutOfBoundsException 。。。)

finally中处理资源的释放(关闭流,关闭socket)

throw throws举例:

当前函数如果用throws声明,则本函数内对抛出的异常不做处理,丢给调用本函数的上层函数去处理。如果抛出的是runtimeException,则不用对抛出的异常处理

举个小例子:

public class Test {
    public static void main(String[] args) {
        try {
            new Test().run();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
   
    public void run() throws Exception{
        int b = -1;
        if(b < 0) {
            throw new Exception("程序出错了");
        }
        System.out.println("running ...");
    }

原文地址:https://www.cnblogs.com/zhangkefan/p/4580595.html