什么是异常?
1 package com.exception; 2 3 public class demo01 { 4 public static void main(String[] args) { 5 new demo01().a(); 6 7 } 8 9 public void a() { 10 b(); 11 } 12 13 public void b() { 14 a(); 15 } 16 } Exception in thread "main" java.lang.StackOverflowError package com.exception; public class demo01 { public static void main(String[] args) { System.out.println(11 / 0); } } Exception in thread "main" java.lang.ArithmeticException: / by zero
简单分类 检查性异常:最具代表性异常是用户错误或问题引起的异常,这是程序员无法预见的,例如要打开一个不存在的文件时,一个异常就发生了,这些异常在编译时不能被简单的忽略。 运行时异常:运行时异常是可能被程序员避免的异常。与检查性异常相反,运行时异常可以在编译时忽略。 错误ERROR:错误不是异常,而实脱离程序员控制的问题。错误在代码中通常被忽略。例如,当栈溢出时,一个错误就发生了,它们在编译时也检查不到。
异常处理框架: Java中把异常当作对象来处理,并定义一个基类java.lang.Throwable作为所有异常的超类. Error和Exception的区别:Error通常是灾难性的致命错误,是程序无法控制和处理的,当这些异常出现时,Java虚拟机(JVM)一般会选择终止线程;Exception通常情况下可以被程序处理,并且在程序中应该尽可能的去处理这些异常。
1. 异常处理机制抛出异常 捕获异常 异常处理五个关键字: try catch finally throw throws 1 package com.exception; 2 3 public class Test { 4 public static void main(String[] args) { 5 int a = 1; 6 int b = 0; 7 try { //try 监控区域 8 System.out.println(a / b); 9 } catch (ArithmeticException e) { //捕获异常 10 System.out.println("程序出现异常,变量b不能为0"); 11 } finally { //处理善后工作,资源的关闭 12 System.out.println("finally"); 13 } 14 15 } 16 } 程序出现异常,变量b不能为0 finally 1 package com.exception; 2 3 public class Test { 4 public static void main(String[] args) { 5 int a = 1; 6 int b = 0; 7 try { //try 监控区域 8 new Test().c(); 9 } catch (Throwable e) { //catch(想要捕获的异常类型) 捕获异常 10 System.out.println("程序出现异常"); 11 } finally { //处理善后工作,资源的关闭 12 System.out.println("finally"); 13 } 14 } 15 16 public void c() { 17 d(); 18 } 19 20 public void d() { 21 c(); 22 } 23 } 程序出现异常 finally
允许多个catch ,就像elseif, 要把最大的Throwable写在最下边, 即从小到大 1 package com.exception; 2 3 public class Test { 4 public static void main(String[] args) { 5 int a = 1; 6 int b = 0; 7 try { //try 监控区域 8 System.out.println(a / b); 9 } catch (Error e) { //catch(想要捕获的异常类型) 捕获异常 10 System.out.println("Error"); 11 } catch (Exception e) { //catch(想要捕获的异常类型) 捕获异常 12 System.out.println("Exception"); 13 } catch (Throwable e) { //catch(想要捕获的异常类型) 捕获异常 14 System.out.println("Throwable"); 15 } finally { //处理善后工作,资源的关闭 16 System.out.println("finally"); 17 } 18 } 19 20 public void c() { 21 d(); 22 } 23 24 public void d() { 25 c(); 26 } 27 } Exception finally
自动生成: ctrl alt + T 在sout后写 1 package com.exception; 2 3 public class Test { 4 public static void main(String[] args) { 5 int a = 1; 6 int b = 0; 7 8 //ctrl alt + T 9 try { 10 System.out.println(a / b); 11 } catch (Exception e) { 12 e.printStackTrace();//打印错误的栈信息,即系统自动生成的 java.lang.ArithmeticException: / by zero 13 } finally { 14 } 15 } 16 }
throw主动的抛出异常,一般在方法中使用 ; throws向上抛出(甩锅) 1 package com.exception; 2 3 public class Test { 4 public static void main(String[] args) { 5 new Test().test(1, 0); 6 } 7 8 //throws 假设这个方法中,处理不了这个异常。在方法上抛出异常 9 public void test(int a, int b) throws ArithmeticException { 10 if (b == 0) { //throw throws 11 throw new ArithmeticException();//主动的抛出异常,一般在方法中使用 12 } 13 System.out.println(a / b); 14 } 15 }
通过使用try catch 可以捕获异常 , 让程序在不终止的情况下 , 继续执行
2. 自定义异常使用java内置的异常类可以描述在编程中出现的大部分异常情况. 1 package com.exception; 2 3 //自定义的异常类 4 public class MyException extends Exception{ 5 6 //传递数字 >10抛出异常 7 8 private int detail; 9 10 public MyException(int a) { //构造器 11 this.detail = a; 12 } 13 14 //toString:异常的打印信息 15 @Override 16 public String toString() { //重写 17 return "MyException{" + 18 "detail=" + detail + 19 '}'; 20 } 21 } 1 package com.exception; 2 3 public class Test { 4 5 //可能会存在异常的方法 6 7 static void test(int a) throws MyException { 8 System.out.println("传递的参数为:" + a); 9 if (a > 10) { 10 throw new MyException(a);//抛出 alt + enter 11 } 12 13 System.out.println("ok"); 14 } 15 16 public static void main(String[] args) { 17 try { 18 test(1); //alt ctrl t 19 } catch (MyException e) { 20 System.out.println("MyException=>" + e); 21 } 22 } 23 } 传递的参数为:1 ok 传递的参数为:11 MyException=>MyException{detail=11}
实际应用中的总结
|
|