一、异常的概念
二、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一个子类就能访问