🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 异常处理 > 用户不正当的输入 > 本身系统的问题 ## 为什么要处理异常 如果不处理异常,一旦在出现异常的地方,程序就会被「异常指令流」直接终止,不再往下运行。 ## 处理异常 **使用 try catch** > 判断一个对象是否为空,使用 `null != str` 在程序方法设计,特别是方法的实现中,对于传入的参数,是不可知的,所以要对传入的参数进行最基础的验证后,才能使用,例如,判断是否为 null。 ~~~ public static void fun1(String str, String subStr) { if (null != str && null != subStr) { if (str.indexOf(subStr) >= 0) { System.out.println("存在子串"); } else { System.out.println("不存在子串"); } } else { System.out.println("不合法字符串"); } } ~~~ 在 try catch 结构中,catch 是可以省略的,也可以多异常的捕获,但是出现多异常的时候,父类异常只能出现在最下面。 在实际的开发中,一般最简单的方式就是使用一个 Exception 直接处理。 **使用 finally** 无论是否出现异常都会被执行,特别要注意的是,如果没有写 catch ,那么 finally 是会被执行的,但是中断后的语句是不会被执行的。 ~~~ public class Demo1 { public static void main(String[] args) { String str = "abc"; str = null; fun1("abc", null); } public static int div(int m, int n) { int k = m / n; return k; } public static void fun1(String str, String subStr) { try { System.out.println("11111111"); int i = 10 / 0; if (str.indexOf(subStr) >= 0) { System.out.println("存在子串"); } else { System.out.println("不存在子串"); } System.out.println("end"); } catch (Exception ex) { System.out.println("程序出现错误"); ex.printStackTrace(); System.out.println("#"+ex.getMessage()); } finally { // 一定会被执行的代码块 System.out.println("finally"); } System.out.println("fun1 end"); } } ~~~ **throws** 在**方法定义**的后面显式的声明方法是有异常的,调用该方法的程序是要显式的处理异常,后者也可以再次抛出。 ~~~ public class Demo2 { public static void main(String[] args) { // fun1(); // fun2(); try { fun3(); } catch (Exception ex) { ex.printStackTrace(); } } public static void fun1() { System.out.println("begin"); int i = 0; try { i = 10 / 0; } catch(Exception ex) { ex.printStackTrace(); } System.out.println(i); System.out.println("end"); } // 使用 throws 抛出异常,通知调用该方法的程序,你要去处理。 public static void fun2() throws Exception{ System.out.println("begin"); int i = 0; i = 10 / 0; System.out.println(i); System.out.println("end"); } public static void fun3() throws Exception { System.out.println("begin"); int i = 0; try { i = 10 / 0; } catch (Exception ex){ // 不做处理 throw new Exception("系统错误"); } System.out.println(i); System.out.println("end"); } } ~~~ **throw** 自定义的创建一个异常对象。在一般的异常发生时候,虚拟机会动态的创建一个「系统异常」抛出,和自定义使用 throw 抛出是一个概念。 ~~~ public class Demo3 { public static void main(String[] args) throws Exception { fun1(17); System.out.println("end"); } public static void fun1(int age) throws Exception { if (age < 18) { throw new Exception("您的年龄不合法"); } else { System.out.println("您已经进入VIP房间"); } } } ~~~ ## 异常堆栈信息 - 异常类 - 异常提示信息:getMessage() - 异常所在的代码位置:自下而上是异常出现的代码所在方法的调用顺序的先后。 ## 常见的异常 NPE:NullPointerException 在调用对象属性或者方法的时候,对象其实是个 null,就会报此异常; java.lang.ArrayIndexOutOfBoundsException:数组越界 java.lang.IndexOutOfBoundsException: Index: 1, Size: 1:集合越界