返回列表 發帖

例外處理 (二) - 自訂例外類別

本帖最後由 tonyh 於 2016-6-17 20:38 編輯

除了捕捉Java拋出的例外物件,還可以利用關鍵字throw自行拋出例外物件。而若拋出的例外物件非系統能自行捕捉到,譬如自訂類別的例外,則需在方法宣告列後面利用關鍵字throws註明例外類別名稱,以便在指定的方法中拋出例外。

  1. import java.util.Scanner;
  2. public class Ch51
  3. {
  4.     static Scanner s=new Scanner(System.in);
  5.     //若拋出的例外物件非系統能自行捕捉到,譬如自訂類別的例外物件,需在此註明
  6.     public static void main(String[] args) throws MyException
  7.     {   
  8.         float x,y;
  9.         System.out.print("輸入分子: ");
  10.         x=s.nextFloat();
  11.         System.out.print("輸入分母: ");
  12.         y=s.nextFloat();
  13.         if(y==0)
  14.             throw new MyException("嘿嘿嘿~分母不可為零喔!");
  15.         System.out.println(x+"/"+y+"="+(x/y));
  16.     }
  17. }
  18. class MyException extends Exception
  19. {
  20.     MyException(String str)
  21.     {
  22.         super(str);   //呼叫父類別的建構子
  23.     }
  24. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. import java.util.Scanner;
  2. public class Ch53
  3. {
  4.     static Scanner s=new Scanner(System.in);
  5.     public static void main(String[] args) throws MyException
  6.     {   
  7.         float x,y;
  8.         System.out.print("輸入分子: ");
  9.         x=s.nextFloat();
  10.         System.out.print("輸入分母: ");
  11.         y=s.nextFloat();
  12.         if(y==0)
  13.             throw new MyException("分母不可為零");
  14.         System.out.println(x+"/"+y+"="+(x/y));
  15.     }
  16. }
  17. class MyException extends Exception
  18. {
  19.     MyException(String str)
  20.     {
  21.         super(str);
  22.     }
  23. }
複製代碼

TOP

本帖最後由 曾挺桂 於 2016-6-17 20:59 編輯

import java.util.Scanner;
public class Ch52
{
    static Scanner s=new Scanner(System.in);
    public static void main(String[] args)throws MyException
    {
        Float x,y;
        System.out.print("輸入分子: ");
        x=s.nextFloat();
        System.out.print("輸入分母: ");
        y=s.nextFloat();
        if(y==0)
            throw new MyException("0!!!!!!!!!!!!");
        System.out.println(x+"/"+y+"="+(x/y));
    }
}
class MyException extends Exception
{
      MyException(String str)
      {
          super(str);
      }
}
class MyException extends Exception
{
      MyException(String str)
      {
          supert(str)
      }
}[/code]

TOP

  1. import java.util.Scanner;
  2. public class Ch51
  3. {
  4.     public static void main(String args[])throws MyException
  5.     {
  6.        float x=0, y=0;
  7.        Scanner s=new Scanner(System.in);
  8.        System.out.print("請輸入一分子:");
  9.        x=s.nextFloat();
  10.        System.out.print("請輸入一分母:");
  11.        y=s.nextFloat();
  12.        if(y==0)
  13.        {
  14.            throw new MyException("北七喔~小時候一定沒專心上課!");
  15.        }
  16.        System.out.println(x+"/"+y+"="+(x/y));

  17.     }
  18. }
  19. class MyException extends Exception
  20. {
  21.     MyException(String str)
  22.     {
  23.         super(str);
  24.     }
  25. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch54
  3. {
  4.     static Scanner s=new Scanner(System.in);
  5.     public static void main(String[] args) throws MyException
  6.     {
  7.         float x,y;
  8.         System.out.print("輸入分子: ");
  9.         x=s.nextFloat();
  10.         System.out.print("輸入分母: ");
  11.         y=s.nextFloat();
  12.         if(y==0)
  13.             throw new MyException("嘿嘿嘿~分母不可為零喔!");
  14.         System.out.println(x+"/"+y+"="+(x/y));
  15.     }
  16. }
  17. class MyException extends Exception
  18. {
  19.     MyException(String str)
  20.     {
  21.         super(str);
  22.     }
  23. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch50{
  3.        public static void main(String args[]) throws MyException{
  4.               int x,y;
  5.               Scanner s=new Scanner(System.in);
  6.               System.out.print("請輸入分子");
  7.               x=s.nextInt();
  8.               System.out.print("請輸入分母");
  9.               y=s.nextInt();
  10.               if(y==0)
  11.                       throw new MyException("分母禁止為0");
  12.               System.out.println("答案為: "+(x/y));

  13.        }
  14. }
  15. class MyException extends Exception{
  16.       MyException(String str){
  17.                        super(str);
  18.       }
  19. }
複製代碼
ABCDEFGHIJKLMNOPQRSTUVWXYZ

TOP

本帖最後由 梁和雋 於 2016-6-17 20:44 編輯
  1. import java.util.Scanner;
  2. public class Ch1010
  3. {
  4.     static Scanner s=new Scanner(System.in);
  5.     public static void main(String[] args) throws MyException
  6.     {
  7.         float x,y;
  8.         System.out.print("輸入分子: ");
  9.         x=s.nextFloat();
  10.         System.out.print("輸入分母: ");
  11.         y=s.nextFloat();
  12.         if(y==0)
  13.             throw new MyException("錯誤啦!");
  14.         System.out.println(x+"/"+y+"="+(x/y));
  15.     }
  16. }
  17. class MyException extends Exception
  18. {
  19.     MyException(String str)
  20.     {
  21.         super(str);
  22.     }
  23. }
複製代碼
http://fs-old.mis.kuas.edu.tw/~s1102137106/music/

TOP

返回列表