Java異常(二)
**********************為什麽有finally*************************
package com.chapter10;
//演示發生了異常沒捕獲的情況
public class TestFinally {
public static void main(String[] args) {
try{
int a = 0;//連接數據庫
int result = 10/a;
System.out.println("aaaaa");
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
System.out.println("數組下標越界");
}catch(NullPointerException e){
e.printStackTrace();
System.out.println("除數不能為0");
}catch(ClassCastException e){
e.printStackTrace();
System.out.println("類型轉換錯誤");
}finally{
//無論如何總要被執行 除非系統強制退出
System.out.println("關閉數據庫");
}
//報錯終止
System.out.println("here");
}
}
程序發生異常後,沒有捕獲 會報錯終止, 這樣後面的代碼就沒有機會執行了(例如關閉數據庫的代碼)..
這樣光申請資源而不釋放肯定是有問題的, 所以為了保證某些代碼 必須被執行 可以把這些代碼 放到
finally代碼塊中, finally代碼塊中的代碼 無論如何都會執行...
一般finally 中放一些釋放資源的代碼 例如關閉數據庫、關閉io流、關閉socket通道等..
關於finally的幾個面試題:
try中如果有return 不會立馬返回, 先執行finally,如果finally中有return 那麽整個方法的返回值取finally中的返回值,
如果finally 中沒有return ,那麽整個方法的返回值取try中的..
*********************try後面必須跟catch 或finally其中一個*************************
package com.chapter10;
//try後面必須跟catch或finally其中之一
public class TestTryPrinciple {
public static void main(String[] args) {
try {
int a = 10;
int b = a/0;
} catch (Exception e) {
System.out.println("算數異常");
e.printStackTrace();
}
}
}
或
try {
int a = 10;
int b = a/0;
} finally{
System.out.println("釋放資源的代碼");
}
*************************************快捷鍵*************************
選中需要加try...catch的代碼塊 右鍵 surround with ---try...catch
****************************處理檢查性異常的第二種方式--使用throws拋出給調用者處理********************
1. 使用try...catch明確處理
駕駛員自己修
2. 使用throws 拋出給調用者處理
坐席碰到異常以後 有兩種處理方式,如果能處理 使用try..catch明確處理,如果處理不了,使用throws拋給調用者處理(上級),
拋給班長坐席,班長坐席收到這個異常以後,又有兩種方式:try..catch 和使用throws繼續拋
演示代碼:
package com.chapter10.handlerCheckedException2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
//演示處理檢查性異常的第二種方式
//使用throws拋給調用者處理
public class TestHandleCheckedException2 {
//警察
//main方法中肯定要處理異常,就像警察必須處理異常一樣
//如果main方法不做任何處理 繼續向上拋出 會拋給JVM(不推薦)
//JVM的默認處理方式: 打印棧軌跡
public static void main(String[] args) throws FileNotFoundException{
TestHandleCheckedException2 test = new TestHandleCheckedException2();
test.laoliu();
}
public void laoliu() throws FileNotFoundException {
manager();
}
public void manager() throws FileNotFoundException {
customerAgentLeader();
}
// 班長坐席
public void customerAgentLeader() throws FileNotFoundException {
customerAgent();
}
public void customerAgent() throws FileNotFoundException {
// 碰到一個異常
FileInputStream fis = new FileInputStream("d:\\jidi16\\aaa.txt");
}
}
********************************查看API**************************************
public FileInputStream(String name)
throws FileNotFoundException
表示該構造函數可能會出現FileNotFoundException異常,並且該方法本身不做處理,拋給調用者處理
使用該類new對象的人需要進行處理或者繼續使用throws拋出
********************************自定義異常**************************************
如果API提供的異常不夠用,需要用戶自己自定義異常
需求: 自定義一個 想要換手機的異常
throw 產生了一個異常 異常發生了 相當於 10/0
演示代碼:
package com.chapter10.演示自定義異常;
//自定義的異常類
//1.繼承Exception
//2.使用super("異常的具體描述") 初始化異常描述
//用戶自己自定義的異常 都是檢查性異常
public class WannaChangePhone extends Exception {
private String name;
private String reason;
public WannaChangePhone() {
super("想要換手機");
}
public WannaChangePhone(String name, String reason) {
super("想要換手機");
this.name = name;
this.reason = reason;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
}
package com.chapter10.演示自定義異常;
//坐席
public class CustomerAgent {
public void getPhone() throws WannaChangePhone{
if(true){
throw new WannaChangePhone("丁澤鵬","買到二手手機");//產生了一個異常
}
}
public static void main(String[] args) {
CustomerAgent ca = new CustomerAgent();
try {
ca.getPhone();
} catch (WannaChangePhone e) {
System.out.println(e.getName() + "因為" + e.getReason() + e.getMessage());
e.printStackTrace();
}
}
}
********************************throws和throw的區別**************************************
throws 處理檢查性異常的第二種方式: 拋給調用者處理, 調用者收到這個異常以後 又有兩種處理方式:
1.使用try..catch明確的處理 2. 使用throws 繼續向上拋
throw 產生了一個異常 異常發生了 相當於10/0
*****************************異常的若幹知識點***************************
1. 當有多個catch塊時, 範圍大的異常類應該放到後面....
instanceof:
引用a instanceof 類b : 如果引用a 引用的是類b或類b子類的對象返回true,否則返回false
2. 不要使用Exception獲取所有異常,雖然這樣寫代碼簡單,但是不能針對不同的異常做不同的處理...區分不開
***************************補充重寫規則之兩小**********************************
1. 子類方法拋出的異常 是 父類方法拋出異常的子類
Java異常(二)