Java-異常處理(預(yù)習(xí))

異常的處理方式
對(duì)于編譯期異常處理有兩種不同的處理方式

1 使用 try  { }  catch { }   finally 語句塊處理它
2 在函數(shù)簽名中使用 throws 聲明 交給函數(shù)調(diào)用者去處理
try     catch    finally  塊

 try     catch    finally  塊 是異常的捕捉,其本質(zhì)是判斷

基本語言如下:

 try{
        可能出現(xiàn)異常的代碼(包括不會(huì)出現(xiàn)異常的代碼)
    } catch (Exception e){
         //括號(hào)里面接收try代碼塊中出現(xiàn)的異常類型 
          如果出現(xiàn)異常時(shí)的處理代碼
    }finally{
        不管代碼是正常執(zhí)行還是出現(xiàn)異常需要處理,finally 代碼塊中的代碼最終都會(huì)執(zhí)行
    }

Main

// try...cath...finally 演示
public class Main {
     public static void main(String[] args) {
         Scanner in = new Scanner(System.in);
         System.out.println("請(qǐng)輸入被除數(shù):\n");
         try {
             int num1 = in.nextInt();
             System.out.println("請(qǐng)輸入除數(shù):\n");
             int num2 = in.nextInt();
             System.out.println(num1 + "/" + num2 + "=" + num1/ num2);
         }catch (Exception e){
             System.out.println("出現(xiàn)錯(cuò)誤,被除數(shù)與除數(shù)必須是整數(shù),除數(shù)不能為零");
             e.printStackTrace();
         }finally {
             in.close();
             System.out.println("感謝使用本程序");
         }
     }

 }
運(yùn)行結(jié)果一.png

TryDemo

public class TryDemo {
    Scanner in = new Scanner(System.in);
    public static void main(String[] args) {
        //使用 throw 和 throws 進(jìn)行演示
        TryDemo demo = new TryDemo();
        try {
            demo.divide();
        } catch (Exception e){
            System.out.println("出現(xiàn)錯(cuò)誤,被除數(shù)與除數(shù)必須是整數(shù),且除數(shù)不能為零");
            e.printStackTrace();
        } finally {
            demo.in.close();
            System.out.println("感謝使用本程序");
        }
    }
    /**
     *
     * 輸入被除數(shù)與除數(shù),計(jì)算結(jié)果并輸入
     *
     * @ throws Exception
     */
    public void divide() throws  Exception{
        System.out.println("請(qǐng)輸入被除數(shù)");
        int num1 = in.nextInt();
        System.out.println("請(qǐng)輸入除數(shù)");
        int num2 = in.nextInt();
        System.out.println(num1+"/"+num2 +"=" + num1/num2);
    }


}
運(yùn)行結(jié)果二.png

Student

public class Student {
    private String name = "";
    private String sex  = "name";
    private int age;

    public String getName(){
        return  name;
    }

    public void setName(String name){
        this.name = name;
    }

    public  String getSex(){
        return  sex;
    }

    public void setSex(String sex) throws Exception{
        if ("name".equals(sex) || "woman".equals(sex)){
            this.sex = sex;
        }else {
            throw new Exception("sex is \"man\"or \"woman\"!");
        }
    }

    public int getAge(){
        return age;
    }

    public  void  setAge(int age){
        this.age = age ;
    }

    public  void  print(){
        System.out.println("我是" + getName() +",性別" + getSex() +",今年" + getAge() + "歲");
    }

    public void getName(String 張三) {
    }

    public void getAge(int i) {
    }
}
/**
 * 捕獲throws 拋出的異常
 */
class Test{
    public static void main(String[] args) {
        Student zhangsan = new Student();
        zhangsan.getName("張三");
        zhangsan.getAge(22);
        try {
            zhangsan.setSex("男性");
        } catch (Exception e){
            e.printStackTrace();
        }
        }
    }
運(yùn)行結(jié)果三.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容