1. 程式人生 > >JavaSE學習總結(八)

JavaSE學習總結(八)

ssa end turn 概念 tac 常用 inpu ram alt

一、理解異常及異常處理的概念

異常就是在程序的運行過程中所發生的不正常的事件,它會中斷正在運行的程序。

異常不是錯誤

程序中關鍵的位置有異常處理,提高程序的穩定性

二、掌握Java異常處理機制

Java的異常處理是通過5個關鍵字來實現的

try:嘗試,把有可能發生錯誤的代碼放在其中,必須有

catch:捕獲,當發生異常時執行

finally:最終,不管是否有異常都將執行

throw:拋出,引發異常

throws:拋出多個,聲明方法將產生某些異常

技術分享

三、掌握try 、catch 、 finally 處理異常

3.1、try..catch

技術分享

技術分享
package com.zhangguo.chapter6.d1;

import java.util.Scanner;

public class Exception1 {

    public static void main(String[] args) {
        
        Scanner input=new Scanner(System.in);
        //int i=input.nextInt();
        int i=Integer.parseInt(input.next());
        System.out.println("您輸入的是:"+i);
        System.out.println("程序結束了");
    }

}
技術分享

異常處理:

技術分享
package com.zhangguo.chapter6.d1;

import java.util.Scanner;

public class Exception1 {

    public static void main(String[] args) {

        try {
            Scanner input = new Scanner(System.in);
            int i = Integer.parseInt(input.next());
            System.out.println("您輸入的是:" + i);
        } catch (Exception exp) {
            System.out.println("發生異常了:" + exp.getMessage());
        }
        System.out.println("程序結束了");
    }

}
技術分享

結果:

技術分享

3.2、try..catch..finally

finally在任何情況下都將執行,正常時會執行,不正常也會執行

技術分享
package com.zhangguo.chapter6.d1;

import java.util.Scanner;

public class Exception1 {

    public static void main(String[] args) {

        try {
            Scanner input = new Scanner(System.in);
            int i = Integer.parseInt(input.next());
            System.out.println("您輸入的是:" + i);
        } catch (Exception exp) {
            System.out.println("發生異常了:" + exp.getMessage());
        }finally {
            System.out.println("輸入結束");
        }
        System.out.println("程序結束了");
    }

}
技術分享

結果:

1
您輸入的是:1
輸入結束
程序結束了

如果用戶輸入是的xyz

技術分享

四、掌握throw 拋出異常、throws 聲明異常

4.1、java中常用的異常

技術分享 常用的異常

4.2、throw..throws

技術分享
package com.zhangguo.chapter6.d1;

public class Exception2 {

    public static void main(String[] args) {
        
        try {
            System.out.println(div(30,3));
        } catch (Exception e) {
            //輸出異常的堆棧信息
            e.printStackTrace();
        }
        
        try {
            System.out.println(div(3,0));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        
    }
    
    //throws 聲明方法將可能拋出異常
    public static int div(int n1,int n2) throws Exception{
        if(n2==0){
            //拋出異常
            throw new Exception("除數不能為零");
        }
        return n1/n2;
    }

}
技術分享

運行結果:

技術分享

五、掌握自定義異常

技術分享
public class ArithmeticException extends RuntimeException {
    private static final long serialVersionUID = 2256477558314496007L;

    /**
     * Constructs an {@code ArithmeticException} with no detail
     * message.
     */
    public ArithmeticException() {
        super();
    }

    /**
     * Constructs an {@code ArithmeticException} with the specified
     * detail message.
     *
     * @param   s   the detail message.
     */
    public ArithmeticException(String s) {
        super(s);
    }
}
技術分享

技術分享

JavaSE學習總結(八)