返回列表 發帖

例外處理 (一)

本帖最後由 tonyh 於 2021-11-6 21:01 編輯

所謂「例外」,就是當程式碼編輯完,在編譯期間沒有出現錯誤訊息,但在程式執行時卻發生錯誤,這種錯誤又稱為執行時期錯誤(Runtime error)。

在編譯程式或執行程式時常會遇到各種不同錯誤,以致無法正常完成工作。研發軟體時最容易遇到三種錯誤(Bug):語法錯誤、執行時期錯誤、邏輯錯誤。

1. 語法錯誤
語法錯誤是初學者最容易犯的錯誤。在編譯過程中,系統常能立即指出此種錯誤的所在,並要求程式設計者修正後才能正式執行。這樣錯誤最容易解決,只要熟悉語法多多練習就可以減少錯誤產生。

2. 執行時期錯誤

程式在執行時,因為輸入資料不符合在計算過程分母為0磁碟中無此檔案存在陣列的索引值超出陣列宣告範圍…等,使得程式中斷執行。這種錯誤的問題在編譯時,並不會發生,被Java稱為「例外」,而Java也提供了例外處理的方式來解決問題。

3. 邏輯錯誤
邏輯錯誤是最難找出的,尤其在大型應用程式最為明顯。程式在執行過程並沒有出現錯誤,也會有執行結果,甚至有時候結果是正確的。除非你仔細觀察,多人多次測試,否則不見得會發現。因此誤信其執行結果,往往造成很大損失。有些系統提供偵錯(Debug)工具,用來協助找出錯誤之處。若沒有偵錯工具,就只能自己設定偵測點,輸出目前主要變數內容是否如預測結果,以推測可能錯誤之處,再仔細研讀程式,尋找邏輯上錯誤之處,加以修正。

試寫一個除法程式, 測試當分母為零, 以及輸入字母時, 產生的例外.

  1. import java.util.Scanner;
  2. public class Ch50
  3. {
  4.     static Scanner s=new Scanner(System.in);
  5.     public static void main(String[] args)
  6.     {   
  7.         int x,y;
  8.         System.out.print("輸入分子: ");
  9.         x=s.nextInt();
  10.         System.out.print("輸入分母: ");
  11.         y=s.nextInt();
  12.         System.out.println(x+"/"+y+"="+(x/y));
  13.     }
  14. }
複製代碼

  1. import java.util.Scanner;

  2. public class Ch01 {

  3.         public static void main(String[] args)
  4.         {
  5.                 Scanner s=new Scanner(System.in);
  6.         int x,y;
  7.                 System.out.print("請輸入x:");
  8.                 x=s.nextInt();
  9.                 System.out.print("請輸入y:");
  10.                 y=s.nextInt();
  11.                 System.out.println("x/y="+x/y);
  12.         }

  13. }
複製代碼

TOP

  1. import java.util.Scanner;


  2. public class Ch50 {

  3.         public static void main(String[] args) {
  4.                 Scanner s=new Scanner(System.in);
  5.                 int x,y;
  6.                 System.out.print("請輸入x: ");
  7.                 x=s.nextInt();
  8.                 System.out.print("請輸入y: ");
  9.                 y=s.nextInt();
  10.                 System.out.println("x/y="+x/y);
  11.         }

  12. }
複製代碼

TOP

  1. import java.nio.channels.ScatteringByteChannel;
  2. import java.util.Scanner;


  3. public class Ch01 {

  4.         public static void main(String[] args) {
  5.                 Scanner s=new Scanner(System.in);{
  6.                 int x,y;
  7.                 System.out.println("x= ");
  8.                 x=s.nextInt();
  9.                 System.out.println("y= ");       
  10.                 y=s.nextInt();
  11.                                 System.out.println("x/y= "+x/y);
  12.                 }
  13.         }

  14. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch50
  3. {
  4.     static Scanner s=new Scanner(System.in);
  5.     public static void main(String[] args)
  6.     {   
  7.         int x,y;
  8.         System.out.print("輸入分子: ");
  9.         x=s.nextInt();
  10.         System.out.print("輸入分母: ");
  11.         y=s.nextInt();
  12.         System.out.println(x+"/"+y+"="+(x/y));
  13.     }
  14. }
複製代碼

TOP

  1. import java.util.Scanner;

  2. public class Ch87 {

  3.         public static void main(String[] args)
  4.         {
  5.                 Scanner s=new Scanner(System.in);
  6.                 int x,y;
  7.                 System.out.print("輸入x: ");
  8.                 x=s.nextInt();
  9.                 System.out.print("輸入y: ");
  10.                 y=s.nextInt();
  11.                 System.out.println("x/y="+x/y);
  12.         }

  13. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch50
  3. {
  4.     static Scanner s=new Scanner(System.in);
  5.     public static void main(String[] args)
  6.     {   
  7.         int x,y;
  8.         System.out.print("輸入分子: ");
  9.         x=s.nextInt();
  10.         System.out.print("輸入分母: ");
  11.         y=s.nextInt();
  12.         System.out.println(x+"/"+y+"="+(x/y));
  13.     }
  14. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch01
  3. {
  4.     static Scanner s=new Scanner(System.in);
  5.     public static void main(String[] args)
  6.     {   
  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.     }
  14. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch01
  3. {
  4.     static Scanner s=new Scanner(System.in);
  5.     public static void main(String[] args)
  6.     {   
  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.     }
  14. }
複製代碼

TOP

  1. import java.util.Scanner;


  2. public class Ch50 {

  3.         public static void main(String[] args) {
  4.                 Scanner s=new Scanner(System.in);
  5.                 int x,y;
  6.                 System.out.print("請輸入x: ");
  7.                 x=s.nextInt();
  8.                 System.out.print("請輸入y: ");
  9.                 y=s.nextInt();
  10.                 System.out.println("x/y="+x/y);
  11.         }

  12. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch20
  3. {
  4.         static Scanner s=new Scanner(System.in);
  5.         public static void main(String[] args)
  6.         {
  7.                 int x,y;
  8.                 System.out.print("輸入分子");
  9.                 x=s.nextInt();
  10.                 System.out.print("輸入分母");
  11.                 y=s.nextInt();
  12.                 System.out.println(x+"/"+y"="+(x/y));

  13.         }
  14. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Ch01
  3. {
  4.         static Scanner s=new Scanner(System.in);
  5.         public static void main(String[] args)
  6.         {       
  7.         int x,y;
  8.                 System.out.print("輸入分子: ");
  9.                 x=s.nextInt();
  10.                 System.out.print("輸入分母: ");
  11.                 y=s.nextInt();
  12.                 System.out.println(x+"/"+y+"="+(x/y));
  13.         }

  14. }
複製代碼

TOP

返回列表