Java中的异常处理简单总结

异常处理

1. 捕获异常

try - catch 结构
try - catch - finally 结构 

格式:
	try {
		// 有可能出现异常代码
	} catch (/* 对应处理的异常对象 */) {
		// 处理方式
	}
package com.qfedu.a_throwable;

/*
 * 捕获异常问题总结:
 * 		1. 代码中从异常发生位置开始,之后的代码都不在运行
 * 		2. 代码中有多个异常,可以使用多个catch块进行捕获操作,分门别类处理
 * 		3. 当前情况下,只能展示异常情况,后期可以讲异常情况做成log日志文件
 * 		4. 异常被捕获之后代码可以正常运行。
 */
public class Demo3 {
	public static void main(String[] args) {
		test(10, 2, null);
	}
	
	public static void test(int num1, int num2, int[] arr) {
		int ret = 0;
		
		try {
			ret = num1 / num2;
			System.out.println("测试代码");
			arr[0] = 10;
		} catch (ArithmeticException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		
		System.out.println("ret:" + ret);
	}
}

2. 抛出异常

throw
	在方法内抛出异常
throws
	在【方法声明】位置,告知调用者当前方法有哪些异常抛出
	声明的异常需要生成对应的文档注释
package com.qfedu.a_throwable;

/*
 * 抛出异常总结:
 * 		1. 一个代码块内,有且只能抛出一个异常
 * 		2. 从throw位置开始,之后的代码不在运行
 * 		3. 代码中存在使用throw抛出异常,在方法的声明位置必须告知调用者这里有什么异常
 *	
 */
public class Demo4 {
	// 调用带有异常抛出的方法,如果选择继续抛出需要在当前方法的声明位置
	// 告知其他调用者,这里有什么异常抛出
	public static void main(String[] args) 
			throws NullPointerException, ArithmeticException {
		// 调用一个带有异常抛出的方法
		
		// 捕获处理
		try {
			test(10, 2, null);
		} catch (ArithmeticException e) {
			System.out.println(e.getMessage());
		} catch (NullPointerException e) {
			System.out.println(e.getMessage());
		}
		
		
		// 可以继续抛出异常
		test(10, 0, null);
	}
	
	/**
	 * 测试方法
	 * 
	 * @param num1 int类型
	 * @param num2 int类型
	 * @param arr int类型数组
	 * @throws ArithmeticException 除数不能为0
	 * @throws NullPointerException 操作数组null地址异常
	 */
	public static void test(int num1, int num2, int[] arr) 
			throws ArithmeticException, NullPointerException {
		// 参数合法性判断
		/*if (0 == num2 || null == arr) {
			System.out.println("Input Parameter is Invalid!");
			return;
		}*/
		// 抛出异常方式来处理当前的参数合法性判断
		if (0 == num2) {
			// 有可能会导致算术异常
			throw new ArithmeticException("算术异常");
		}
		
		if (null == arr) {
			// 存在数组操作空指针异常
			throw new NullPointerException("数组空指针异常");
		}
		
		System.out.println(num1 / num2);
		arr[0] = 10;
	}
}

3. 抛出和捕获的对比

捕获之后,代码可以正常运行,要保证处理之后的异常不会在导致其他问题。
例如:
	用户名密码错误,不能采用捕获异常。
	用户指定路径问题,也不能采用捕获异常。

抛出的确可以解决很多问题,并且可以让代码健壮性很强。到用户层面说什么都不能抛出异常。
	所谓不能抛出,是指不能讲错误信息直接甩到用户脸上。

用户密码错误情况:
	1. 捕获异常
	2. 通过异常处理 catch将错误抛出
	3. 给予用户的友好提示

4. 自定义异常

代码运行的过程中存在一定的生活化
	例如:
		用户名密码错误
		NoGirlFriendException 自定义异常

自定义异常格式:
	class 自定义异常类名 extends Exception {
		// No Fields Constructor
		
		// String Field Constructor
	}
	自定义异常类名:
		必须Exception结尾!!!
	```
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值