Java异常捕获及处理
异常的捕获及处理
1、异常处理
-
异常处理格式
try{ // 有可能出现异常的语句 }catch(异常类 异常对象){ // 编写异常的处理语句 }[catch(异常类 异常对象){ // 编写异常的处理语句 }catch(异常类 异常对象){ // 编写异常的处理语句 }...] [finally{ // 一定会运行到的程序代码 }] -
异常处理流程
-
对异常进行捕获
public class ExceptionDemo01 { public static void main(String[] args) { System.out.println("*********计算开始*********"); int i = 10; int j = 0; try{ int temp = i / j; // 此处会产生异常 System.out.println("两个数相处的结果:" + temp); System.out.println("--------------------------"); }catch(ArithmeticException e){ System.out.println("出现了异常:" + e); }finally { System.out.println("不管出不出现异常都会执行这里!"); } System.out.println("**********计算结束**********"); } } 输出: *********计算开始********* 出现了异常:java.lang.ArithmeticException: / by zero 不管出不出现异常都会执行这里! **********计算结束********
2、异常类的继承结构
-
异常结构
Java的异常结构中最常用的两个类,Exception和Error。这两个类都是Throwable的子类。
- Exception:一般表示的是程序中出现的问题,可以直接使用try....catch处理。
- Error:一般指的是JVM错误,程序中无法处理。
开发者习惯于将Exception和Error统称为异常,算术异常、数字格式异常等都属于Exception的子类。
-
异常信息的输出方式
- 在catch语句中直接使用:
System.out.println(异常对象)
- 直接使用Exception类中的:
printStackTrace()方法 例如:e.printStackTrace();
这种方式打印的异常信息最完整。
- 在catch语句中直接使用:
3、Java的异常处理机制
-
Java的异常处理步骤
- 一旦产生异常,则首先会产生一个异常类的实例化对象。
- 在try语句中对此异常对象进行捕获。
- 产生的异常对象与catch语句中的各个异常类型进行匹配,如果匹配成功,执行catch语句中的代码。
-
使用Exception处理异常
注意:在Java中所有捕获范围小的异常必须放在捕获范围大的异常之前,否则程序在编译时会出现错误。
可以直接使用Exception进行异常处理:
public class ExceptionDemo01 { public static void main(String[] args) { System.out.println("*********计算开始*********"); int i = 10; int j = 0; try{ int temp = i / j; // 此处会产生异常 System.out.println("两个数相处的结果:" + temp); System.out.println("--------------------------"); }catch(Exception e){ System.out.println("出现了异常:" + e); }finally { System.out.println("不管出不出现异常都会执行这里!"); } System.out.println("**********计算结束**********"); } } 输出: *********计算开始********* 出现了异常:java.lang.ArithmeticException: / by zero 不管出不出现异常都会执行这里! **********计算结束********** 以上代码直接使用了Exception进行异常处理,所以任何的异常都可以非常方便地进行处理。
4、throws与throw关键字
-
throws关键字
定义方法时可以使用throws关键字声明,使用throws声明的意义是:表示此方法不处理异常,而是交给方法的调用处进行处理。
如果方法中可能会出现异常就可以使用throws关键字进行声明。
使用格式:
public 返回值类型 方法名称(参数列表) throws 异常类{}
案例:
class Math{ public int div(int i, int j) throws Exception{ //方法可以不处理异常 int temp = i / j; return temp; } } public class ClassDemo01 { public static void main(String[] args){ Math math = new Math(); // 使用了throws关键字,所有在调用方法的地方一定要处理异常。 try{ System.out.println("除法操作:" + math.div(10, 2)); }catch (Exception e){ e.printStackTrace(); } } } 注意:
主方法处使用throws关键字
因为主方法是一个程序的起点,需要在向上抛出异常,也就是只能将异常抛给JVM进行处理。
不要在主方法中使用throws
主方法是一切的起点,所以,如果在主方法中使用throws,则程序出现问题后,就会交给JVM进行处理,这将导致程序的中断。class Math{ public int div(int i, int j) throws Exception{ //方法可以不处理异常 int temp = i / j; return temp; } } public class ClassDemo01 { //主方法使用throws声明后,后面所有的异常都可以不用try...catch进行处理,因为后交给JVM进行处理。 public static void main(String[] args) throws Exception{ Math math = new Math(); System.out.println("除法操作:" + math.div(10, 2)); } -
throw关键字
throw关键字是人为的抛出一个异常,直接抛出异常类的实例化对象。public class ClassDemo01 { public static void main(String[] args){ try{ throw new Exception("自己抛出的异常"); }catch (Exception e){ System.out.println(e); } } } 输出: java.lang.Exception: 自己抛出的异常 理解:
异常产生时肯定会由系统产生一个异常类的实例化对象,只是此时异常类的实例化对象是手工产生的。
-
throws 和 throw 的综合应用
class Math{ public int div(int i, int j) throws Exception{ //方法处可以不处理异常 System.out.println("*****计算开始*****"); int temp = 0; try { temp = i / j; }catch (Exception e){ //捕获异常 throw e; //把异常交给被调用处 }finally{ System.out.println("*****计算结束*****"); } return temp; } } public class ClassDemo01 { public static void main(String[] args){ Math math = new Math(); try { System.out.println("除法操作:" + math.div(10, 0)); } catch (Exception e) { //进行异常捕获 System.out.println("异常产生" + e); } } }
5.Exception类与RuntimeException类
区别:
- Exception:在程序中必须使用try...catch进行处理。
- RuntimeException:可以不使用try...catch进行处理,但是如果有异常产生,则异常将由JVM进行处理。
举例:
字符串转换为整型:
public class ClassDemo01 { public static void main(String[] args){ String str = "123"; int temp = Integer.parseInt(str); //这里就没有使用 try...catch处理 System.out.println(temp * temp) } } 可以查看parseInt()方法的声明:
public static int parseInt(String s) throws NumberFormatException{}
查看NumberFormatException异常类的继承关系:
6.自定义异常类
定义异常类只需要继承Exception类即可
案例:
自定义异常类:
class MyException extends Exception{ // 构造方法接收异常信息 public MyException(String msg){ // 调用父类中的构造方法 super(msg); } } public class ClassDemo01 { public static void main(String[] args){ try{ throw new MyException("自定义异常。"); }catch (Exception e){ System.out.println(e); } } }
7.断言
格式:
assert boolean 表达式;
assert boolean 表达式 :详细信息;
说明:
如果以上boolean表达式的返回值为true,则什么错误信息都不会提示;如果为false,则会提示错误信息;如果没有详细说明错误信息,则系统使用默认的错误信息提示方式。
举例:
public class ClassDemo01 { public static void main(String[] args){ int[] x = {1, 2, 3}; // 此处断言数组长度为0,肯定是错误的 assert x.length == 0; } }
public class ClassDemo01 { public static void main(String[] args){ int[] x = {1, 2, 3}; // 此处断言数组长度为0,肯定是错误的 assert x.length == 0:"数组长度不为0"; } }
注意:
程序在正常运行时断言并不会起任何的作用,如果想要让断言起作用,则需要在java运行时加入以下参数:
-enableassertions --> 可以简写为 "-ea"
使用命令:
编译程序: javac Test.java
运行程序: java -ea Test
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战