8.2 异常处理机制
在Java中异常的处理就分文异常的抛出和捕获
异常处理的五个关键字
-
try
尝试去处理异常,try可以监控区域,也就是在这个代码块区域,产生的异常就可以捕获的到
-
catch
捕获异常,捕获异常要先知道异常的类型,也就是如果try中出现了ArithmeticException异常,就执行这个代码块内容
-
finally
无论是否执行最终都要执行的代码
-
throw
抛出异常,主动抛出一个异常
在方法内部使用throw一个异常,使用new关键词,在方法内部抛出的异常什么都不会做,抛出异常一般会在“方法中”时用
方法内使用throw抛出异常
package com.baidu.www.exception; public class Test2 { public static void main(String[] args) { int a = 1; int b = 0; //Ctrl+alt+t try { if(b==0){ throw new ArithmeticException();//主动抛出异常 } System.out.println(a/b); } catch (Exception e) { e.printStackTrace();//这句换是自动生成的是打印错误的栈信息 } finally { } } } //设么都不会做
在方法中抛出异常会抛出异常
package com.baidu.www.exception; public class Test2 { public static void main(String[] args) { new Test2().test(1,0); } public void test(int a, int b){ if(b==0){ throw new ArithmeticException();//主动抛出异常 } System.out.println(a/b); } } /** * Exception in thread "main" java.lang.ArithmeticException * at com.baidu.www.exception.Test2.test(Test2.java:10) * at com.baidu.www.exception.Test2.main(Test2.java:5) * * Process finished with exit code 1 */这里如果不做除法运算,主动抛出异常也会抛出异常
-
throws
抛出异常,这个是在方法上面用的。
如果方法中处理不了这个异常,就需要将这个异常抛出更高的级别,需要在方法上抛出异常,这就需要用关键字throws,在方法上抛出的异常被调用时可以使用try–catch来捕获和这个异常并处理。主动在方法中抛出异常是throw,在方法上是throws。
这里就告诉我们需要随时去应对突发状况,算术异常属于运行时异常,这种异常不需要程序抛出,他自己就会抛出,即使程序中不用try–catch去捕获处理,程序遇到这种错误也会停止,但是用了try–catch之后程序捕获之后依旧会继续运行,假设程序异常在意料之中可以不用停止程序执行而是继续往下走,让异常或者错误在catch中给他处理掉就好了。
方法上使用throws抛出异常,对比上面throws抛出异常运行结果可以看出
package com.baidu.www.exception; public class Test2 { public static void main(String[] args) { try { new Test2().test(1,0); } catch (ArithmeticException e) { System.out.println("程序出错"); } finally { } } public void test(int a, int b) throws ArithmeticException{ if(b==0){ throw new ArithmeticException();//主动抛出异常 } System.out.println(a/b); } } /** * 程序出错 * * Process finished with exit code 0 */
try、catch是必须要有的东西,可以不要finally,代码中可能出现多种异常,可以写多个catch,捕获不同种类的异常,就像if-else一样层层递进,当然异常要一步一步的扩大,也就是上下要有扩大的关系,也就是Throwable要放在最下面的捕获异常,也就是说要捕获多个异常需要从小到大捕获。finally在后期遇到一些IO流,资源等相关的需要关闭的,就像scanner需要关闭的操作就放在finally中
简单异常处理机制示例
package com.baidu.www.exception;
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
try{//try可以监控区域,也就是在这个代码块区域,产生的异常就可以捕获的到
System.out.println(a/b);
}catch (ArithmeticException e){
/**
* 捕获异常要先知道异常的类型
* 也就是如果try中出现了ArithmeticException异常,就执行这个代码块内容
*/
System.out.println("程序出现异常,变量b不能为0");
}finally {//最后处理善后工作
System.out.println("finally");
}
/**
* try、catch是必须要有的东西,可以不要finally
* finally在后期遇到一些IO流,资源等相关的需要关闭的,就像scanner需要关闭的操作就放在finally中
*/
}
}
多一个异常捕获
package com.baidu.www.exception;
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
try{//try可以监控区域,也就是在这个代码块区域,产生的异常就可以捕获的到
new Test().a();
}catch (ArithmeticException e){
/**
*catch(想要捕获的异常类型,最高的就是Throwable)中的参数是想要捕获的异常类型
*/
System.out.println("程序出现异常");
}catch (Exception e){
System.out.println("Exception");
}catch (Throwable e){
System.out.println("Throwable");
}
finally {//最后处理善后工作
System.out.println("finnally");
}
/**
* try、catch是必须要有的东西,可以不要finally
* finally在后期遇到一些IO流,资源等相关的需要关闭的,就像scanner需要关闭的操作就放在finally中
*/
}
public void a(){b();}
public void b(){a();}
}
/**
* Throwable
* finnally
*
* Process finished with exit code 0
*/
IDEA中自动支持自动生成捕获异常代码,选中可能会出现异常的代码,按下快捷键Ctrl+alt+t
package com.baidu.www.exception;
public class Test2 {
public static void main(String[] args) {
int a = 1;
int b = 0;
//Ctrl+alt+t
try {
System.out.println(a/b);
} catch (Exception e) {
e.printStackTrace();//这句换是自动生成的是打印错误的栈信息
} finally {
}
}
}