Board logo

標題: 例外處理 (二) [打印本頁]

作者: tonyh    時間: 2018-12-17 20:58     標題: 例外處理 (二)

進行例外處理是不希望程式中斷。而是希望程式能捕捉錯誤並繼續執行,若錯誤是使用者輸入不正確資料所造成的,可以要求使用者輸入正確資料再繼續執行,或者不處理使用者輸入資料繼續做其他工作。

Java使用 try … catch … finally 敘述來解決例外處理,它的方式是將被監視的敘述區段寫在try大括號內,當程式執行到try內的敘述有發生錯誤時,會逐一檢查捕捉(catch)該錯誤,以便執行該catch內敘述。最後不管是否有符合catch,都會執行最後的finally敘述區段。例外處理的格式如下:

try
{
    預期可能發生例外的敘述
}
catch(例外物件)
{
    對應的處理程序
}
finally       //可有可無
{
    無論例外是否發生都會處理的程序
}


試運用無窮迴圈搭配 try...catch 語法捕捉例外, 使執行時無論發生什麼例外, 程式都不會因此而中斷. 另外使用 toString() 方法將捕捉到的例外顯示出來.

  1. import java.util.Scanner;
  2. public class Ch52
  3. {

  4.     public static void main(String[] args)
  5.     {
  6.         for(;;)
  7.         {
  8.             try
  9.             {
  10.                 Scanner s=new Scanner(System.in);
  11.                 int x,y;
  12.                 System.out.print("輸入分子: ");
  13.                 x=s.nextInt();
  14.                 System.out.print("輸入分母: ");
  15.                 y=s.nextInt();
  16.                 System.out.println(x+"/"+y+"="+(x/y));
  17.             }catch(Exception e)
  18.             {
  19.                 System.out.println("程式發生錯誤!");
  20.                 System.out.println("例外類別: "+e.toString());
  21.             }
  22.             System.out.println();
  23.         }
  24.     }
  25. }
複製代碼

作者: 洪翊庭    時間: 2018-12-24 19:26

  1. import java.util.Scanner;
  2. public class Ch17
  3. {

  4.     public static void main(String[] args)
  5.     {
  6.         for(;;)
  7.         {
  8.             try
  9.             {
  10.                 Scanner s=new Scanner(System.in);
  11.                 int a,b;
  12.                 System.out.print("輸入分子: ");
  13.                 a=s.nextInt();
  14.                 System.out.print("輸入分母: ");
  15.                 b=s.nextInt();
  16.                 System.out.println(a+"/"+b+"="+(a/b));
  17.             }catch(Exception e)
  18.             {
  19.                 System.out.println("程式發生錯誤!");
  20.                 System.out.println("例外類別: "+e.toString());
  21.             }
  22.             System.out.println();
  23.         }
  24.     }
  25. }
複製代碼

作者: 黃宇綸    時間: 2018-12-24 19:27

  1. import java.util.Scanner;
  2. public class Ch01
  3. {

  4.     public static void main(String[] args)
  5.     {
  6.         for(;;)
  7.         {
  8.             try
  9.             {
  10.                 Scanner s=new Scanner(System.in);
  11.                 int x,y;
  12.                 System.out.print("輸入分子: ");
  13.                 x=s.nextInt();
  14.                 System.out.print("輸入分母: ");
  15.                 y=s.nextInt();
  16.                 System.out.println(x+"/"+y+"="+(x/y));
  17.             }catch(Exception e)
  18.             {
  19.                 System.out.println("程式發生錯誤!");
  20.                 System.out.println("例外類別: "+e.toString());
  21.             }
  22.             System.out.println();
  23.         }
  24.     }
  25. }
複製代碼

作者: 洪翊展    時間: 2018-12-24 19:28

  1. import java.util.Scanner;
  2. public class Ch01
  3. {
  4.   public static void main(String[] args)
  5.     {
  6.         for(;;)
  7.         {
  8.             try
  9.             {
  10.               Scanner s=new Scanner(System.in);
  11.               int x,y;
  12.               System.out.print("輸入分子: ");
  13.                 x=s.nextInt();
  14.               System.out.print("輸入分母: ");
  15.                 y=s.nextInt();
  16.                System.out.println(x+"/"+y+"="+(x/y));
  17.             }catch(Exception e)
  18.              {
  19.                 System.out.println("程式發生錯誤!");
  20.                 System.out.println("例外類別: "+e.toString());
  21.             }
  22.             System.out.println();
  23.          }

  24.      }
  25. }
  26.                
  27.                
  28.                
  29.                
複製代碼

作者: 李沛昂    時間: 2018-12-24 19:28

  1. import java.util.Scanner;
  2. public class Ch52
  3. {

  4.     public static void main(String[] args)
  5.     {
  6.         for(;;)
  7.         {
  8.             try
  9.             {
  10.                 Scanner s=new Scanner(System.in);
  11.                 int x,y;
  12.                 System.out.print("輸入分子: ");
  13.                 x=s.nextInt();
  14.                 System.out.print("輸入分母: ");
  15.                 y=s.nextInt();
  16.                 System.out.println(x+"/"+y+"="+(x/y));
  17.             }catch(Exception e)
  18.             {
  19.                 System.out.println("程式發生錯誤!");
  20.                 System.out.println("例外類別: "+e.toString());
  21.             }
  22.             System.out.println();
  23.         }
  24.     }
  25. }
複製代碼

作者: 黃宇瑄    時間: 2018-12-24 19:29

  1. import java.util.Scanner;
  2. public class Ch01
  3. {

  4.     public static void main(String[] args)
  5.     {
  6.         for(;;)
  7.         {
  8.             try
  9.             {
  10.                 Scanner s=new Scanner(System.in);
  11.                 int x,y;
  12.                 System.out.print("輸入分子: ");
  13.                 x=s.nextInt();
  14.                 System.out.print("輸入分母: ");
  15.                 y=s.nextInt();
  16.                 System.out.println(x+"/"+y+"="+(x/y));
  17.             }catch(Exception e)
  18.             {
  19.                 System.out.println("程式發生錯誤!");
  20.                 System.out.println("例外類別: "+e.toString());
  21.             }
  22.             System.out.println();
  23.         }
  24.     }
  25. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2