Java面向对象,异常处理

78 阅读1分钟

异常处理

异常是在程序中导致程序中断运行的一种指令流

public class ExceptionDem01{ pubic static void mian(String argsp[]){ int i = 10; int j = 0 System.out.println(===计算开始====) int temp =i/j; System.out.println(temp = " + temp); System.out.println(==计算结束===); } }

异常处理:

try{ }catch(异常类型1 对象名1){ }catch(异常类型2 对象名2){ } ... finally{} public class ExceptionDemo01{ public static void main(String argsp[]){ int i = 10 ; int j = 0 ; System.out.println("============= 计算开始 =============") ; try{ int temp = i / j ; System.out.println("temp = " + temp) ; System.out.println("============= 计算结束 =============") ; }catch(ArithmeticException e){ System.out.println("除数不能为零") ; } } };

异常体系结构:

异常指的是Exception , Exception类, 在Java中存在一个父类Throwable(可能的抛出)

Throwable存在两个子类:

Error:表示的是错误,是JVM发出的错误操作,只能尽量避免,无法用代码处理。

Exception:一般表示所有

多异常捕获的注意点:

1、 捕获更粗的异常不能放在捕获更细的异常之前。

2、 如果为了方便,则可以将所有的异常都使用Exception进行捕获。