java物件導向第六章

來源:互聯網
上載者:User

標籤:情況   語句   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物件導向第六章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.