1、概念:
错误,不正常
2、生活中异常:
停水,停电,生病…
3、Java中异常:
程序错误导致程序无法正常完成,得到程序员预想的结果。
4、健壮程序:
每个程序基本都有异常,可能在某个环境没有出现而已,这时程序员应该具有处理异常能力,提前预判该程序可能出现的异常,通过处理异常程序不会因为异常而导致无法正常运行,该程序就是一个健壮的程序,好的程序,鲁棒性强的程序。
5、异常机制:
public class Demo7_异常机制 {
public static void main(String[] args) {
//需求:请两个整数的商
int num1 = 9;
int num2 = 0;
int res = getDiv(num1, num2);
System.out.println(res);
System.out.println("程序结束");
}
public static int getDiv(int num1, int num2) {
return num1 / num2;
}
/*
Exception in thread "main" java.lang.ArithmeticException: / by zero
at day14.Demo7_异常机制.getDiv(Demo7_异常机制.java:16)
at day14.Demo7_异常机制.main(Demo7_异常机制.java:9)
解析:Exception in thread "main":该异常出现在主线程中
java.lang.ArithmeticException:该异常的类型是ArithmeticException 算术异常
/ by zero:该异常的描述
at day14.Demo7_异常机制.main(Demo7_异常机制.java:9) 该异常所在方法调用位置
at day14.Demo7_异常机制.getDiv(Demo7_异常机制.java:16) 该异常所在方法出现位置
*/
}

5、异常处理
(1)处理异常
1)语法
1、try-catch
try{
可能出现异常代码
}catch(异常类型){
处理该异常类型的代码
}
public class Demo7_异常机制 {
public static void main(String[] args) {
//需求:请两个整数的商
int num1 = 9;
int num2 = 0;
int res = getDiv(num1, num2);
System.out.println(res);
System.out.println("程序结束");
}
public static int getDiv(int num1, int num2) {
try {
return num1 / num2;
}catch(ArithmeticException ex){
return 0;//如果出现异常就返回0
}
}
}
2、try-catch-finally
try{
可能出现异常代码
}catch(异常类型){
处理该异常类型的代码
}finally{
最后执行的代码
}
public class Demo7_异常机制 {
public static void main(String[] args) {
//需求:请两个整数的商
int num1 = 9;
int num2 = 0;
int res = getDiv(num1, num2);
System.out.println(res);
System.out.println("程序结束");
}
public static int getDiv(int num1, int num2) {
try {
return num1 / num2;
}catch(ArithmeticException ex){
//System.exit(0);
return 0;//如果出现异常就返回0
}finally{
System.out.println("wahaha");
}
//注意点1:方法中存在return和finally块,这时就算return结束方法,但仍然会执行finally块。
//如果硬要finally块不执行,可以使用System.exit(0)实现。
//注意点2:finally块由于不受return结束方法影响,仍然会执行,所以一般用于释放资源使用。
}
}
3、try-catch[-catch]-finally 多路捕获异常
try{
可能出现异常代码
}catch(异常类型){
处理该异常类型的代码
}catch(异常类型){
处理该异常类型的代码
}…
finally{
最后执行的代码
}
public class Demo7_异常机制 {
public static void main(String[] args) {
//需求:访问数组元素
int[] arr = {1, 2, 3};
try { //alt+contrl+t
System.out.println(arr[2]);
arr = null;
System.out.println(arr[0]);
} catch (IndexOutOfBoundsException e) {
//.printStackTrace() 用于打印异常类属性数据
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {//所有异常类型的父类
e.printStackTrace();
}
//注意1:针对无法提前识别的异常,可以使用Exception来捕获该异常。
//注意2:使用Exception捕获异常,该catch块必须是最后一个,否则出现错误。
}
}
4、try-finally
try{
需要释放资源代码
}finally{
最后执行的代码
}
(2)声明异常
1)概念:告诉给该方法的调用者,该方法有可能抛出什么类型异常,请处理。
2)语法
访问修饰符 返回值类型 方法名(参数列表) throws 异常类型1,异常类型2,…{
方法体
}
throws:是关键字,表示声明
异常类型1,异常类型2,…:一个方法可以同时抛出多个异常类型
public class Demo8_声明异常 {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
try {
method1(arr, 3);
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
//声明异常作用:
// 1、定义方告诉调用方,该方法可能出现异常有哪些。
// 2、调用方可以根据自己的需求对异常提出对应解决方法。
System.out.println("程序结束");
}
private static void method1(int[] arr, int index) throws IndexOutOfBoundsException, NullPointerException {
System.out.println(arr[index]);
arr = null;
System.out.println(arr[index]);
}
}
3)注意点
import java.io.IOException;
import java.sql.SQLException;
class Fu {
public void method() throws RuntimeException,SQLException {
System.out.println("Fu method...");
}
}
class Zi extends Fu {
@Override
public void method() throws RuntimeException,SQLException {
System.out.println("Zi method%%%");
}
}
public class Demo8_声明异常 {
public static void main(String[] args) {
Zi zi = new Zi();
zi.method();
//注意点
//1、方法重写中,父类方法声明异常,子类重写方法可以不声明异常
//2、方法重写中,父类方法没有声明异常,子类也不能声明异常
//3、方法重写中,父类方法声明异常,子类重写方法只能不声明异常或声明该异常或其子类异常。
//4、方法重写中,父类方法声明多个异常,子类重写方法只能不声明异常或声明相同多个异常或声明相同的部分异常。
System.out.println("程序结束");
}
}
6、自定义异常
(1)异常框架

(2)自定义异常类
//编译异常
class AgeException extends Exception{
//目的:可以自定义描述信息
public AgeException(String message) {
super(message);
}
}
class Human{
private int age;
public int getAge() {
return age;
}
public void setAge(int age) throws AgeException {
if(age<0||age>100){
//对错误数据进行处理
//this.age = 18;
//问题:没有一种异常描述年龄问题,如何使用异常解决呢?
//答:自定义异常
throw new AgeException("年龄只能在0到100岁之间");
}else{
this.age = age;
}
}
}
public class Demo9_自定义异常 {
public static void main(String[] args) {
//需求:输入人的年龄信息
Human human = new Human();
try {
human.setAge(200);
} catch (AgeException e) {
//.getMessage() 只输出异常描述信息
System.out.println(e.getMessage());
}
System.out.println(human.getAge());
}
}
本文介绍了Java中异常的概念,即程序错误导致无法正常完成预期结果。强调程序员应具备处理异常的能力,使程序更健壮。详细阐述了异常处理的语法,如try - catch、try - catch - finally等,还介绍了声明异常的概念、语法及注意点,最后提及了自定义异常。
9407

被折叠的 条评论
为什么被折叠?



