装修网站建设价格,成都必去的地方排行,泉州建设网站公司哪家好,想学网站制作目录
异常处理
1. 如何处理异常
2. throw 抛出异常
语法
示例
3. throws 声明可能抛出的异常类型
语法
示例
4. try-catch 捕获异常
语法
示例
思考#xff1a;如果一个方法可能抛出多个异常#xff0c;如何捕获#xff1f;
示例
5. finally 语句
语法
示例…目录
异常处理
1. 如何处理异常
2. throw 抛出异常
语法
示例
3. throws 声明可能抛出的异常类型
语法
示例
4. try-catch 捕获异常
语法
示例
思考如果一个方法可能抛出多个异常如何捕获
示例
5. finally 语句
语法
示例
面试题
Java SE文章参考:Java SE入门及基础知识合集-CSDN博客 异常处理
1. 如何处理异常 在Java 中异常的种类很多如果每一种异常类型我们都需要去记住这无疑是一件很困难的事。如果能够有一套机制来处理异常那么将减少程序员在开发时的耗时。Java 就提供了一套异常处理机制来出来异常。Java 处理异常使用了 5 个关键字 throw 、 throws 、 try 、 catch 、 finally 2. throw 抛出异常 throw关键字只能在方法内部使用 throw 关键字抛出异常表示自身并未对异常进行处理。 语法 throw 异常对象 ; // 通常与 if 选择结构配合使用 示例 package com . we . exception ; import java . util . Scanner ; public class Example1 { private static Scanner sc new Scanner ( System . in ); public static void main ( String [] args ) { calculate (); } public static void calculate (){ System . out . println ( 请输入一个数字 ); int number1 sc . nextInt (); System . out . println ( 请再输入一个数字 ); int number2 sc . nextInt (); if ( number2 0 ){ ArithmeticException e new ArithmeticException ( 在除法运算中除数不能为零 ); throw e ; } int result number1 / number2 ; System . out . println ( result ); // 该行代码不能够被执行 } } 3. throws 声明可能抛出的异常类型 throws关键字只能应用在 方法或者构造方法的定义上 对可能抛出的异常类型进行声明自身不会对异常做出处理由方法的调用者来处理。如果方法的调用者未处理则异常将持续向上一级调用者抛出直至main() 方法为止如果 main() 方法也未处理那么程序可能因此终止 语法 访问修饰符 返回值类型 方法名 ( 参数列表 ) throws 异常类型 1 , 异常类型 2 ,... 异常类型 n { } 示例 package com .we . exception ; import java . util . InputMismatchException ; import java . util . Scanner ; public class Example2 { private static Scanner sc new Scanner ( System . in ); public static void main ( String [] args ) { int result devided (); System . out . println ( result ); } public static int devided () throws InputMismatchException , ArithmeticException { int number1 getNumber (); int number2 getNumber (); return number1 / number2 ; } /** * * return * throws InputMismatchException 执行该方法时可能会抛出 InputMismatchException */ public static int getNumber () throws InputMismatchException { System . out . println ( 请输入一个数字 ); int number sc . nextInt (); return number ; } } throws 可以声明方法执行时可能抛出的异常类型但需要注意的是方法执行过程中只能抛出声明的异常类型的其中一个异常。 4. try-catch 捕获异常 throw 和 throws 关键字均没有对异常进行处理这可能会导致程序终止。在这种情况下可以使用 trycatch结构来对抛出异常进行捕获处理从而保证程序能够正常运行。 语法 try { //代码块 } catch ( 异常类型 异常对象名 ){ } 其中try 表示尝试的意思尝试执行 try 结构中的代码块如果执行过程中抛出了异常则交给 catch 语句块进行捕获操作 示例 package com .we . exception ; import java . util . InputMismatchException ; import java . util . Scanner ; public class Example3 { private static Scanner sc new Scanner ( System . in ); public static void main ( String [] args ) { try { int number getNumber (); System . out . println ( number ); } catch ( InputMismatchException e ){ e . printStackTrace (); // 打印异常轨迹 System . out . println ( e . getClass (). getName ()); System . out . println ( 异常信息 e . getMessage ()); System . out . println ( 请不要瞎搞只能数数字 ); } System . out . println ( 发生异常也会执行 ); } /** * * return * throws InputMismatchException 执行该方法时可能会抛出InputMismatchException */ public static int getNumber () throws InputMismatchException { System . out . println ( 请输入一个数字 ); int number sc . nextInt (); return number ; } } 思考如果一个方法可能抛出多个异常如何捕获 多以在try 后面添加多个 catch 子句来分别对每一种异常进行处理。 示例 package com . we . exception ; import java . util . InputMismatchException ; import java . util . Scanner ; public class Example3 { private static Scanner sc new Scanner ( System . in ); public static void main ( String [] args ) { int result devided (); System . out . println ( result ); } public static int devided () { try { int number1 getNumber (); int number2 getNumber (); return number1 / number2 ; } catch ( InputMismatchException e ){ System . out . println ( 请不要瞎搞只能数数字 ); return - 1 ; } catch ( ArithmeticException e ){ System . out . println ( 在除法运算中除数不能为零 ); return - 2 ; } } /** * * return * throws InputMismatchException 执行该方法时可能会抛出 InputMismatchException */ public static int getNumber () throws InputMismatchException { System . out . println ( 请输入一个数字 ); int number sc . nextInt (); return number ; } } 当使用多个catch 子句捕获异常时如果捕获的多个异常对象的数据类型具有继承关系那么父类异常 不能放在前面。 5. finally 语句 finally 语句不能单独使用必须与 try 语句或者 try-catch 结构配合使用表示无论程序是否发生异常都会执行主要用于释放资源。但 如果在 try 语句或者 catch 语句中存在系统退出的代码则 finally 语 句将得不到执行。 System . exit ( 0 ); // 系统正常退出 0- 正常退出 非 0- 异常退出 System . exit ( 1 ); // 系统异常退出 语法 try { } finally { } // 或者 try { } catch ( 异常类型 异常对象名 ){ } finally { } 示例 package com . we . exception ; public class Example4 { private static int [] numbers { 1 , 2 , 3 , 4 , 5 }; public static void main ( String [] args ) { try { int number getNumberFromArray ( 5 ); System . out . println ( number ); } catch ( ArrayIndexOutOfBoundsException e ){ System . out . println ( 数组下标越界了 ); System . exit ( 0 ); } finally { System . out . println ( 需要执行的代码 ); } } public static int getNumberFromArray ( int index ){ return numbers [ index ]; } } 面试题 分析如下代码的执行结果 public class Example5 { public static void main ( String [] args ) { int result getResult (); System . out . println ( result ); } public static int getResult (){ int number 10 ; try { // 尝试执行 //返回值 尝试返回一个结果但发现后面还有 finally 模块而 finally 模块一定会得到执行。于是在这里只能将 //返回值使用一个临时变量( 例如变量 a) 存储起来。然后再执行 finally 模块finally模块执行完之后再将这个临时 //变量(a) 存储的值返回 return number ; } catch ( Exception e ){ return 1 ; } finally { number ; } } } Java SE文章参考:Java SE入门及基础知识合集-CSDN博客