java各种异常总结

一、java异常汇总

1.Throwable 是所有异常的祖先,Throwable有两个子类,Error和Exception;

2.Error是错误,表示运行应用程序中出现了严重错误,都是通过Error抛出的,一般程序无法处理;

Exception是异常,表示程序运行的时候,程序本身可以捕获并可以处理的错误;

3.运行时异常:RuntimeException

(1)NullPointerException(空指针异常)

(2)ClassCastException(类型转换异常)

(3)ArithmeticException(算术异常)——除数为0的算术异常

(4)IllegalArgumentException(非法的参数异常)

NubmerFormatException(数字格式化异常)

(5)IllegalStateException(非法状态异常)

(6)IndexOutOfBoundsException(下标越界异常)

ArrayIndexOutOfBoundsException(数组下标越界异常)

StringIndexOutOfBoundsException(字符串下标越界异常)

(7)NoSuchElementException(没有这样的元素异常)

InputMisMatchException(输入类型不匹配异常)

4.编译时异常

(1)IOException(输入\输出异常)

FileNotFoundException(文件未找到异常)

EOFException(EOF异常)

MalformedURLException(格式错误的 URL 异常)

UnknownHostException(未知主机异常)

(2)SQLException(SQL异常)

(3)CloneNotSupportedException(不支持克隆异常)

(4)ReflectiveOperationException(反射操作异常)

ClassNotFoundException(类未找到异常)

二、捕获异常:

try:执行可能产生异常的代码;

catch:捕获异常;

finally:无论是否发生异常,代码总被执行;

抛出异常:

throw:异常生成阶段,手动抛出异常对象;

声明异常:

throws声明方法可能要抛出的各种异常类

public class Test {

    public static void main(String[] args) throws Exception {

        try {

            System.out.println("test");

            throw new Exception("字符串越界");

        } catch (Exception e) {

            System.out.println(e.getMessage());

            System.out.println(e.toString());

            throw new Exception(e.getMessage());

        } finally {

            System.out.println("finally");

        }

    }

}

打印结果:

test

字符串越界

java.lang.Exception: 字符串越界

finally

java各种异常总结_第1张图片

 

你可能感兴趣的:(Java进阶,java,异常,Exception)