8.9
package com.zhongruan.test;
import java.util.ArrayList;
import java.util.List;
public class TestException {
public static void main(String[] args) {
//1.ArithmeticException
//2.ArrayIndexOutOfBoundsException
//3.Null![在这里插入图片描述](https://img-blog.csdnimg.cn/2019081209185385.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3pyemNzbA==,size_16,color_FFFFFF,t_70)PointerException
/**
try {
int i = 2 / 0;
System.out.println("111111111");
}catch(ArithmeticException e){
System.out.println("除数不能为0 ");
}finally {
System.out.println("22222222222");
}
**/
div(1,0);
// Class.forName("11111");
}
static int div(int a,int b) throws ArithmeticException{
int c=a/b;
return c;
}
//异常处理方式: 1.jvm 处理 运行java程序的抽象计算机 异常名称 异常信息 异常出现的位置
// 2.自己处理 (1) try catch finally
// (2) throws
//所有异常的父类都是Exception
//异常的类型
//1.运行时异常 extends RuntimeException
//2.编译异常 必须自己处理
}
举例子:
package com.zhongruan.test;
public class Tset123 {
public static void main(String[] args) throws AgeException {
Person person=new Person();
try{
person.setAge(-1);
}catch (AgeException e) {
e.printStackTrace();
}
//int age = person.getAge();
//System.out.println(age);
}
}
package com.zhongruan.test;
public class AgeException extends Exception{
public AgeException(String message){
super(message);
}
}