標籤:情況 語句 mat int test 儲存日誌 代碼 項目 final
一、 異常
1、 什麼是異常?
java程式在運行過程中出現的意外情況
2、 java中如何進行異常處理?
java中的異常處理機制
try、catch、finally、throw、throws
try{
//有可能出現異常的代碼
}catch(異常類型){
//對出現的異常進行捕獲和處理
return;
//System.exit(1);// finally語句塊將不執行
}finally{
//不管程式是否發生異常,都要執行的代碼
}
try…catch…finally一共有3種組合方式
try…catch…(catch可以有多種,但要注意先子類後父類的順序)
try…catch…finally…
try…finally….
public class Test3 { private static Logger logger=Logger.getLogger(Test3.class.getName()); public static void main(String [] args){ Scanner input=new Scanner(System.in); System.out.print("請輸入被除數:"); int num1=input.nextInt(); System.out.println(""); System.out.print("請輸入除數:"); int num2=input.nextInt(); try{ System.out.println(num1/num2); System.out.println("感謝使用本程式!"); }catch(InputMismatchException e){ logger.error("出現錯誤!除數和被除數必須為整數!",e); }catch(ArithmeticException e){ logger.error(e.getMessage()); }catch(Exception e){ logger.error(e.getMessage()); }finally { System.out.println("歡飲您使用本程式!"); } }}樣本
3、 常見的異常類型?表6-1
4、 throw和throws2個關鍵字都是用於拋出異常
區別有3點,P149頁
作用不同
位置不同
內容不同,
二、 log4j:1款開源的日誌記錄工具
使用步驟:
1、 在項目中添加log4j .jar檔案
2、 建立log4j.propterties檔案【儲存日誌資訊的相關設定】
3、 編寫日誌的配置資訊【輸出層級、目的地、格式】
4、 在程式中,使用log4j記錄日誌(sql日誌、異常日誌、業務日誌等3種類型的日誌資訊)
java物件導向第六章