Java基础案例教程4.5———异常

一、异常的概念

 

 

 

 

 

二、try...catch 和 finally

package cn.itcast.chapter04.example21;

public class Example21 {
	public static void main(String[] args) {
		// 下面的代码定义了一个try…catch语句用于捕获异常
		try {
			int result = divide(4, 0); // 调用divide()方法
			System.out.println(result);
		} catch (Exception e) { // 对异常进行处理
			System.out.println("捕获的异常信息为:" + e.getMessage());
		}
		System.out.println("程序继续向下执行...");
	}

	// 下面的方法实现了两个整数相除
	public static int divide(int x, int y) {
		int result = x / y; // 定义一个变量result记录两个数相除的结果
		return result; // 将结果返回
	}
}

 

 

 

无论是否有异常,都会进入finally ,然后再结束

finally一般用于释放资源

package cn.itcast.chapter04.example22;

public class Example22 {
	public static void main(String[] args) {
		// 下面的代码定义了一个try…catch…finally语句用于捕获异常
		try {
			int result = divide(4, 0); // 调用divide()方法
			System.out.println(result);
		} catch (Exception e) { // 对捕获到的异常进行处理
			System.out.println("捕获的异常信息为:" + e.getMessage());
			return; // 用于结束当前语句
		} finally {
			System.out.println("进入finally代码块");
		}
		System.out.println("程序继续向下执行…");
	}

	// 下面的方法实现了两个整数相除
	public static int divide(int x, int y) {
		int result = x / y; // 定义一个变量result记录两个数相除的结果
		return result; // 将结果返回
	}
}
捕获的异常信息为:/ by zero
进入finally代码块

 

package cn.itcast.chapter04.example22;

public class Example22 {
	public static void main(String[] args) {
		// 下面的代码定义了一个try…catch…finally语句用于捕获异常
		try {
			int result = divide(4, 2); // 调用divide()方法
			System.out.println(result);
		} catch (Exception e) { // 对捕获到的异常进行处理
			System.out.println("捕获的异常信息为:" + e.getMessage());
			return; // 用于结束当前语句
		} finally {
			System.out.println("进入finally代码块");
		}
		System.out.println("程序继续向下执行…");
	}

	// 下面的方法实现了两个整数相除
	public static int divide(int x, int y) {
		int result = x / y; // 定义一个变量result记录两个数相除的结果
		return result; // 将结果返回
	}
}
2
进入finally代码块
程序继续向下执行…

 

 

三、throws关键字

 

如果调用了throws, 必须得进行处理

1、try...catch

package cn.itcast.chapter04.example24;
public class Example24 {
	public static void main(String[] args) {
		// 下面的代码定义了一个try…catch语句用于捕获异常
		try {
			int result = divide(4, 2); // 调用divide()方法
			System.out.println(result);
		} catch (Exception e) { // 对捕获到的异常进行处理
			e.printStackTrace(); // 打印捕获的异常信息
		}
	}
	// 下面的方法实现了两个整数相除,并使用throws关键字声明抛出异常
	public static int divide(int x, int y) throws Exception {
		int result = x / y; // 定义一个变量result记录两个数相除的结果
		return result; // 将结果返回
	}
}
java.lang.ArithmeticException: / by zero
	at cn.itcast.chapter04.example24.Example24.divide(Example24.java:14)
	at cn.itcast.chapter04.example24.Example24.main(Example24.java:6)

 

2、虚拟机(main) 处理

package cn.itcast.chapter04.example25;

public class Example25 {
	public static void main(String[] args) throws Exception {
		int result = divide(4, 0); // 调用divide()方法
		System.out.println(result);
	}

	// 下面的方法实现了两个整数相除,并使用throws关键字声明抛出异常
	public static int divide(int x, int y) throws Exception {
		int result = x / y; // 定义一个变量result记录两个数相除的结果
		return result; // 将结果返回
	}
}

 

四、运行时异常和编译异常

 

 

 

五、自定义异常

 

package cn.itcast.chapter04.example27;

// 下面的代码是自定义一个异常类继承自Exception
public class DivideByMinusException extends Exception {
	public DivideByMinusException() {
		super(); // 调用Exception无参的构造方法
	}

	public DivideByMinusException(String message) {
		super(message); // 调用Exception有参的构造方法
	}
}

 

package cn.itcast.chapter04.example27;

public class Example27 {
	public static void main(String[] args) {
		// 下面的代码定义了一个try…catch语句用于捕获异常
		try {
			int result = divide(4, -2); // 调用divide()方法,传入一个负数作为被除数
			System.out.println(result);
		} catch (DivideByMinusException e) { // 对捕获到的异常进行处理
			System.out.println(e.getMessage()); // 打印捕获的异常信息
		}
	}

	// 下面的方法实现了两个整数相除,并使用throws关键字声明抛出自定义异常
	public static int divide(int x, int y) throws DivideByMinusException {
		if (y < 0) {
			throw new DivideByMinusException("除数是负数");// 使用throw关键字声明异常对象
		}
		int result = x / y; // 定义一个变量result记录两个数相除的结果
		return result; // 将结果返回
	}
}

 

 

六、访问控制级别

 

protected:在子类中用 "super." 才能访问,不是说 new一个子类就能访问

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值