try - catch 必须搭配使用,不能单独使用。finlly子句:与 try - catch语句连用,无论try - catch 语句是否执行顺利,finlly 语句都会被执行。html
代码格式1:java
try { //可能出现异常的代码 } catch (异常类型 变量名) { //异常处理代码 } finally { //必定会执行的代码,如关闭资源 }
执行顺序:数据库
代码格式2:
try - catch 能够同时存在多个catch 语句数组
try { } catch (异常类型1 变量名1) { } catch (异常类型2 变量名2) { } ...... catch (异常类型n 变量名n) { }
代码中发生异常,异常被抛给第一个catch块,若是不匹配则继续往下一个catch进行传递
注: 一个try代码块后面跟多个catch代码块的状况就叫多重捕获函数
代码格式3:
在Java7之前,每一个catch语句块只能捕获一种异常,从Java7开始就支持一个catch捕获多种异常,多个异常之间用|隔开。日志
try{ //可能会产生异常的代码 } catch(Exception1 | Exception2 |... | Exception_n e1){ //统一处理的异常代码 } finally{ //一般是释放资源的代码 }
public class ExceptionDemo { public static void main(String[] args) { try { // 除数不能为0,此行会抛出 ArithmeticException 异常 int i = 10 / 0; // 抛出异常后,此行不会执行 System.out.println("i = " + i); } catch (ArithmeticException e) { // 捕获 ArithmeticException 异常 // 异常最详细信息 e.printStackTrace(); // 发生异常的缘由 System.out.println("e.getMessage() : " + e.getMessage()); // 获取异常的类型和异常描述信息 System.out.println("e.toString() : " + e.toString()); } } }
public class ExceptionDemo { public static void main(String[] args) { // try-catch-finally搭配使用 try { int[] arr = {1,2,3}; // 数组索引越界,此行会抛出 ArrayIndexOutOfBoundsException 异常 int i = arr[3]; // 抛出异常后,此行不会执行 System.out.println("i = " + i); }catch(ArithmeticException e) { System.out.println(e.getMessage()); }finally { System.out.println("finally"); } // try-finally搭配使用 try { int[] arr = {1,2,3}; int i = arr[3]; System.out.println("i = " + i); }finally { System.out.println("finally"); } } }
public class trycatch { public static void main(String[] args) { try { int a =10 / 0; System.out.println("a:"+a); }catch (Exception e) { System.out.println("出现异常"); }finally { System.out.println("finally"); } System.out.println("end"); } }
输出结果:code
出现异常错误htm
finallyblog
end索引
解释: try程序段发生异常后,执行catch段,而后执行finally段,而后顺寻执行try - catch - finally段后面的程序。
public class trycatch { public static void main(String[] args) { // TODO Auto-generated method stub try { //写尝试要执行的代码 int a =10 / 0; System.out.println("a:"+a); }catch (Exception e) { System.out.println("出现异常错误"); return; }finally { System.out.println("finally"); } // 程序return,try - finally 代码块后续代码不会执行 System.out.println("end"); } }
输出结果:
出现异常错误
finally
解释: try程序段发生异常后,执行catch段,而后执行finally段,而后执行catch段的return
public static void main(String[] args) { try { int a =10 /10; System.out.println("a:"+a); return; }catch (Exception e) { System.out.println("出现异常错误"); }finally { System.out.println("finally"); } // 程序return,try - finally 代码块后续代码不会执行 system.out.println("end"); } }
输出结果:
a:1
finally
解释: try - catch - finally 语句中有return语句时,会执行 finally 语句内容但不会执行 try - catch - finally 语句以外的代码。
public class ExceptionDemo { public static void main(String[] args) { // try-catch-finally搭配使用 try { int[] arr = {1,2,3}; // 数组索引越界,此行会抛出 ArrayIndexOutOfBoundsException 异常 int i = arr[3]; // 抛出异常后,此行不会执行 System.out.println("i = " + i); }catch(ArithmeticException e) { System.out.println(e.getMessage()); // 程序强制退出,finally 代码块不会执行 System.exit(0); }finally { System.out.println("finally"); } // 程序强制退出,try - finally 代码块后续代码不会执行 system.out.println("end"); } }
做者:快乐随行