返回列表 發帖

例外處理 (二)

本帖最後由 tonyh 於 2021-11-6 20:53 編輯

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

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. }
複製代碼

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

  24. }
複製代碼

TOP

  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. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch02
  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. }
複製代碼

TOP

  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. }
複製代碼

TOP

  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. }
複製代碼

TOP

  1. import java.util.Scanner;


  2. public class Ch01 {

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

  21.         }

  22. }
複製代碼

TOP

  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.printIn(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. }
複製代碼

TOP

  1. import java.util.Scanner;


  2. public class Ch01 {

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

  22. }
複製代碼

TOP

  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. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch22 {

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


  25. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch02
  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. }
複製代碼

TOP

  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. }
複製代碼

TOP

返回列表