返回列表 發帖

例外處理 (二)

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

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 aaa {

  3.         public static void main(String[] args) {
  4.                 // TODO 自動產生的方法 Stub
  5.                 Scanner c=new Scanner(System.in);
  6.                 while(true)
  7.                 {
  8.                     try
  9.                     {
  10.                         Scanner s=new Scanner(System.in);
  11.                         int x,y;
  12.                         System.out.print("輸入分子: ");
  13.                         x=c.nextInt();
  14.                         System.out.print("輸入分母: ");
  15.                         y=c.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 Hello {

  3.         public static void main(String[] args) {
  4.                 // TODO 自動產生的方法 Stub

  5.                 Scanner s=new Scanner(System.in);
  6.                 try
  7.                 {
  8.                     int x ,y;
  9.                     System.out.print("請輸入分子:");
  10.                     x=s.nextInt();
  11.                     System.out.print("請輸入分母:");
  12.                     y=s.nextInt();
  13.                     System.out.println("x/y="+(x/y));
  14.                 }catch(Exception tim)
  15.                 {
  16.                         System.out.println("出錯了");
  17.                         System.out.println("在"+tim.toString()+"  錯了");
  18.                 }finally
  19.                 {
  20.                         System.out.println("?!?!");
  21.                 }
  22.         }

  23. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Dc
  3. {
  4.   public static void main(String args[])
  5.   {
  6.     try{
  7.     Scanner a=new Scanner(System.in);
  8.     int x,y;
  9.     System.out.println("請輸入分子x");
  10.     x= a.nextInt();
  11.     System.out.println("請輸入分母y");
  12.     y= a.nextInt();
  13.     System.out.print("x/y="+(x/y));
  14.     }
  15.     catch(Exception exc)
  16.     {
  17.       System.out.println("有誤");
  18.       System.out.println("例外:"+exc.toString());
  19.     }

  20.   }
  21. }
複製代碼

TOP

  1. import java.util.Scanner;

  2. public class java01 {

  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("輸入分子");
  11.                                 x = s.nextInt();
  12.                                 System.out.print("輸入分母");
  13.                                 y = s.nextInt();
  14.                                 System.out.println(x + "/" + y + "=" + (x / y));
  15.                         } catch (Exception e) {
  16.                                 System.out.println("程式發生錯誤");
  17.                                 System.out.println("例外類別"+e.toString());
  18.                         }
  19.                         System.out.println();
  20.                 }

  21.         }
  22. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class work
  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 ch04
  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# 陳品肇

TOP

  1. import java.util.Scanner;
  2. public class Haha
  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.     }
  18.     catch(Exception e)
  19.     {
  20.         System.out.println("程式發生錯誤!");
  21.         System.out.println("例外類別: "+e.toString());
  22.     }   
  23.     System.out.println();
  24.         }
  25.     }
  26. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch01
  3. {
  4.     public static void main(String[] args)
  5.     {   
  6.         for(;;)
  7.         {
  8.                 Scanner s=new Scanner(System.in);
  9.                     try
  10.                     {
  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.                     }
  18.                     catch(Exception e)
  19.                     {
  20.                             System.out.println("程式發生錯誤!");
  21.                 System.out.println("例外類別: "+e.toString());
  22.                     }
  23.                     System.out.println();
  24.         }
  25.     }
  26. }
複製代碼

TOP

返回列表