异常处理方法:
抛出异常 -> 捕获异常 -> 处理异常
自行处理(积极处理):
try
{
可能发生异常的语句;
}
catch(异常类型 一场引用名)
{
处理异常的语句;
}
finally
{
最终都要执行的语句,常用于释放或关闭资源;
}
注意:
1.try语句块与catch语句块不可分割,是一个完整的语句结构
2.可以同时定义多个catch语句块,用来捕获不同的异常
3.当多个catch语句同时定义时,必须根据捕获的异常类型从小到大排序
4.finally语句:无论代码执行return,或者break,还是发生了具体的异常,始终都会执行,除非执行System.exit(0);退出了系统,finally才不会执行
回避处理(消息处理):
throws:修饰方法,告诉调用方法的人,该方法可能存在某类异常
throw:抛出具体的异常
例子:
try...catch
package com.lqh.chapter06; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class _39trycatch { public static void catchException() { try { // 1.文件找不到 FileInputStream input = new FileInputStream(new File("你好.txt")); // 2.空指针异常 String str = null; System.out.println(str.length()); // 3.数组下标越界异常 int[] list = new int[] { 1, 2, 3, 4, 5 }; for (int i = 0; i <= list.length; i++) { System.out.println(list[i]); } // 4.算数异常 int num = 1 / 0; } // JDK1.7新特性,允许一个catch中同时定义多个异常类型 catch (FileNotFoundException | NullPointerException | ArrayIndexOutOfBoundsException | ArithmeticException e) { System.out.println("发生异常了:" + e.getMessage()); } // // 1.负责捕获“文件找不到”异常 // catch (FileNotFoundException e) { // e.printStackTrace();//打印异常堆栈信息 // System.out.println("你的文件<你好.txt>找不到");//一般记录日志,或放松邮件、短信 // } // //2.负责捕获“空指针”异常 // catch(NullPointerException e) { // //e.printStackTrace();//打印异常堆栈信息 // System.out.println("调用length()方法的对象str为null,不能使用"); // } // //3.负责捕获“数组下表越界异常” // catch(ArrayIndexOutOfBoundsException e) { // //e.printStackTrace();//打印异常堆栈信息 // System.out.println("当前数组的长度为5,最大下标为4,不能输出第六个元素"); // } // //4.负责捕获“算数异常” // catch(ArithmeticException e) { // //e.printStackTrace();//打印异常堆栈信息 // System.out.println("除数不能为0"); // } // 5.负责捕获所有的“运行时异常” catch (RuntimeException e) { System.out.println("发生了运行时异常"); } // 6.负责捕获所有异常(包括运行时异常和非运行时异常) catch (Exception e) { System.out.println("发生了异常"); } finally { System.out.println("始终都要执行的代码"); } } public static void main(String[] args) { catchException(); } }
throws:
package com.lqh.chapter06; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class _40throws { // 告诉调用方法的人,该方法可能存在某类异常 public static void throwsException() throws FileNotFoundException, NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException { // 1.文件找不到 FileInputStream input = new FileInputStream(new File("你好.txt")); // 2.空指针异常 String str = null; System.out.println(str.length()); // 3.数组下标越界异常 int[] list = new int[] { 1, 2, 3, 4, 5 }; for (int i = 0; i <= list.length; i++) { System.out.println(list[i]); } // 4.算数异常 int num = 1 / 0; } public static void main(String[] args) { try { throwsException(); } // 1.负责捕获“文件找不到”异常 catch (FileNotFoundException e) { e.printStackTrace();//打印异常堆栈信息 System.out.println("你的文件<你好.txt>找不到");//一般记录日志,或放松邮件、短信 } //2.负责捕获“空指针”异常 catch(NullPointerException e) { //e.printStackTrace();//打印异常堆栈信息 System.out.println("调用length()方法的对象str为null,不能使用"); } //3.负责捕获“数组下表越界异常” catch(ArrayIndexOutOfBoundsException e) { //e.printStackTrace();//打印异常堆栈信息 System.out.println("当前数组的长度为5,最大下标为4,不能输出第六个元素"); } //4.负责捕获“算数异常” catch(ArithmeticException e) { //e.printStackTrace();//打印异常堆栈信息 System.out.println("除数不能为0"); } } }
throw:
package com.lqh.chapter06; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class _41throw { // 告诉调用方法的人,该方法可能存在某类异常 public static void throwException() throws FileNotFoundException, NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException { // 1.文件找不到 try { FileInputStream input = new FileInputStream(new File("nihao.txt")); // 2.空指针异常 String str = null; System.out.println(str.length()); // 3.数组下标越界异常 int[] list = new int[] { 1, 2, 3, 4, 5 }; for (int i = 0; i <= list.length; i++) { System.out.println(list[i]); } // 4.算数异常 int num = 1 / 0; } // 1.负责捕获“文件找不到”异常 catch (FileNotFoundException e) { throw e; } // 2.负责捕获“空指针”异常 catch (NullPointerException e) { throw e; } // 3.负责捕获“数组下表越界异常” catch (ArrayIndexOutOfBoundsException e) { throw e; } // 4.负责捕获“算数异常” catch (ArithmeticException e) { throw e; } } public static void main(String[] args) { try { throwException(); } catch (ArrayIndexOutOfBoundsException | FileNotFoundException | NullPointerException | ArithmeticException e) { System.out.println(e.getMessage()); } } }