异常机制

Throwable类

Java中每个异常都是Throwable类的一个实例
在这里插入图片描述

Error :

  • 这类错误一般是虚拟机生产或脱出的,程序员无法控制,不需要管

Exception:

CheckedException:

  • 编译时异常||检查时异常 必须进行处理,否则程序无法运行,发生在编译期

RuntimeException:

  • 运行时异常 程序运行才知道是否有异常

常见的一小部分运行时异常:

  • 1.NullPointerException 空指针异常
  • 2.ArrayIndexOutOfBoundsException 数组下标越界
  • 3.NegativeArraySizeException 负数异常
  • 4.ArithmeticException 数学异常
  • 5.StringIndexOutOfBoundsException 字符串索引越界异常
单词对应意义
try监听异常
catch捕获异常
throw语句抛出异常
throws方法抛出异常

语句:

try catch finally语句

try{
	可能有异常的代码||被测试代码||被监听的代码
} catch ( 查找的异常类型1 ) {
	出现对应异常执行的代码1
} catch ( 查找的异常类型2 ) {
	出现对应异常执行的代码2
} ...
  catch ( Exception e ) {
  	接收了以上异常之外的其他异常执行的代码
}finally{
	一定会执行的代码
}
//测试 try catch finally语句
public class ExceptionTest {
	public static void main(String[] args) {
		try {
			int[] array =null;
			System.out.println(array[1]);
			int[] arr1 = {2,3,45,6,67,8,9,8,6,8,};
			System.out.println(arr1[15]);
			System.out.println(1/0);
			int[] arr=new int[-3];	
			//捕获了异常就不执行后面的代码
		}catch(StringIndexOutOfBoundsException e){
			System.out.println("字符串索引越界异常");
		}catch(NullPointerException e){
			System.out.println("空指针异常");
		}catch(ArrayIndexOutOfBoundsException e){
			System.out.println("数组下标越界");
		}catch(ArithmeticException e){
			System.out.println("数学异常");
		}catch(NegativeArraySizeException e){
			System.out.println("负数异常");			
		/*}catch(ArrayIndexOutOfBoundsException e){//相当于 A a = new A;
			System.out.println("数组下标越界");*/
		}catch(Exception e){//有点像switch语句里的default
			System.out.println("系统运行异常 包含其他及以上异常");
		}finally{//重要的需要执行的语句
			System.out.println("无论测试语句是否报错我都会执行");
		}
		
	}
}

throw
throw要么和try-catch-finally语句配套使用,要么与throws配套使用
语法:
throw (异常对象);
throw e;


throws
throws可以单独使用
语法:
throws(异常类)-------用在方法的方法名和方法体之间
public void sleep(int a) throws Exception1,Exception3{…}

//测试throws&&throw
public class ThrowsTest {
	public static void main(String[] args) {
		try {
			TestThrows();			
		} catch (ArithmeticException e) {
			System.out.println("测试throws捕获了ArithmeticException异常");
		}
		try{
			TestThrow();
		}catch(ArithmeticException e){
			System.out.println("测试throw捕获了ArithmeticException异常");
		}	
	}//测试throws
	static void TestThrows ()throws ArithmeticException{//将异常抛到调用处
		System.out.println(23.4/0);
		System.out.println(-23.4/0);
		System.out.println(24/0);//ArithmeticException
		System.out.println(24);
	}//测试throw
	static void TestThrow(){//将异常抛到调用处
		System.out.println(234/0);
		//throw new ArithmeticException();//111  测试111和222都可以
		ArithmeticException arithmeticException = new ArithmeticException();//222
		throw arithmeticException;//222
	}
}

自定义异常: ☆☆☆

除了java提供的异常类以外,可以自定义异常

1:测试类 try catch 接收测试代码 并在catch中提供打印效果

try {
被测试代码
} catch (异常类名 e) {
System.out.println(e.getMessage());
e.printStackTrace();   //核心效果
}System.out.println(被测试的类);

2.自定义的异常类

定义一个extends自Exception || RuntimeException的类

private String message;
...
public String getMessage() {
		return message;    //catch输出调用

3.被测试类

主要提供抛出异常
throws
throw


4.toString

最好提供重写的String类中的toString方法


代码示例:

//测试自定义异常
public class AgeException extends Exception{
	public static void main(String[] args) {
		// 测试类
		Persion persion = new Persion();
		persion.setName("张三");// 设置名字
		try {// 监听年龄
			persion.setAge(18);
			// 设置年龄
			/* persion.setAge(158);//设置年龄
			 *  persion.setAge(18);//设置年龄*/		 
		} catch (AgeException e) {// 获取异常 == new
			System.out.println(e.getMessage());//通过变量调用 getMessage获取信息
			e.printStackTrace();//调用printStackTrace方法出现类似于系统异常的打印效果
		}
		System.out.println(persion);// 打印信息
	}
}

// 定义一个年龄范围的异常
class AgeException extends Exception {
	// 定义变量message
	private String message;

	// 空参构造器
	public AgeException() {
	}

	// 带参构造器
	public AgeException(String message) {
		this.message = message;
	}

	// 提供一个返回信息方法
	public String getMessage() {
		return message;
	}//实际效果第一行 和第三行 的接收效果
}

// 创建一个Persion类
class Persion {
	// 创建变量
	private String name;
	private int age;

	// 空构造器
	public Persion() {
		super();
	}

	// 带参构造器
	public Persion(int age, String name) {
		this.name = name;
		this.age = age;
	}

	// 添加设置器和访问器
	public int getAge() {
		return age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}// 在年龄设置器里判断

	public void setAge(int age) throws AgeException {// 方法抛出异常
		if (age > 0 && age < 110) {// 正常年龄
			this.age = age;
		} else {// 抛出一个 带参的 自定义带参实例
			throw new AgeException(age + "年龄异常");//小括号里的是实际效果第一行 和第三行 的输入
		}
	}

	// 重写toString方法//测试信息用的
	@Override
	public String toString() {
		return "Persion [name=" + name + ", age=" + age + "]";//打印的人物信息
	}
}

// ******************************************************************
//实际效果

/*
异常效果:

-18年龄异常
Persion [name=张三, age=0]
ExceptionTest.AgeException: -18年龄异常
	at ExceptionTest.Persion.setAge(CustomRuntimeException.java:77)
	at ExceptionTest.CustomRuntimeException.main(CustomRuntimeException.java:10)

正常效果:

Persion [name=张三, age=18]

//*********************************************************************
 */

异常处理:

异常类型方法1方法2方法3
编译时异常:throwstry…catch
运行时异常:throwstry…catch增强健壮性(比如 if else)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值