# 異常處理
> 用戶不正當的輸入
> 本身系統的問題
## 為什么要處理異常
如果不處理異常,一旦在出現異常的地方,程序就會被「異常指令流」直接終止,不再往下運行。
## 處理異常
**使用 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:集合越界
- 前言
- 計算機概論
- 數據庫
- 數據庫介紹
- MySQL的安裝
- SQL
- 表基本操作
- 修改數據語句
- 數據檢索操作
- 多表數據操作
- 表結構設計
- 綜合應用
- JAVA
- JAVA 介紹
- JAVA 運行原理
- JDK 配置
- 類和對象
- 數據類型
- 變量
- 直接量
- 運算符
- 流程控制
- 數組結構
- 面向對象
- 隱藏和封裝
- 深入構造器
- 類的繼承
- 多態
- 包裝類
- final 修飾符
- 抽象類
- 接口
- 集合框架
- 常用類學習
- 異常處理
- 設計模式-單例模式
- JDBC
- JSP&Servlet
- Web應用
- Tomcat
- JSP
- Scriptlet
- Page 指令
- 包含指令
- 跳轉指令
- 用戶注冊實例
- JSP練習
- 內置對象
- Servlet
- 過濾器
- Web分層思想
- EL表達式
- JSTL
- 分頁實現
- AJAX&JSON
- 開發步驟
- 路徑問題
- Log4j
- 電子書城
- 案例分析
- 核心代碼
- Java 高級
- 文件操作
- 泛型
- 類加載機制和反射
- 注解 Annotation
- Mybatis框架
- 框架介紹
- Mybatis簡單實現
- 表基本操作
- 優化配置文件
- 表字段名與實體類屬性名不同的解決方案
- 一對一關聯
- 一對多關聯
- 教學管理
- 學員名錄
- 周測統計
- 2017-10-27
- 2017-11-03
- 2017-11-10
- 2017-11-17
- 課堂作業
- 班會紀要
- 2017-10-24
- 缺勤記錄
- 班級備忘錄
- 違紀統計
- 編程素養
- Day001
- Day002
- Day003
- Day004
- Day005
- Day006
- Day007
- Day008
- Day009
- Day010
- Day011
- Day012
- Day013
- Day014
- Day015
- Day016
- Day017
- Day018
- Day019