try - catch 必须搭配使用,不能单独使用。finlly子句:与 try - catch语句连用,不管try - catch 语句是否执行顺利,finlly 语句都会被执行。
代码格式1:
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");
}
}
输出结果:
出现异常错误
finally
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");
}
}
作者:快乐随行
https://www.cnblogs.com/jddreams/p/14281918.html
JAVA 中的异常(2)- 异常的处理方式一:try-catch
原文:https://www.cnblogs.com/jddreams/p/14281918.html