java小白——try-catch語句中的while循環問題

無限循環問題

在今天的課堂練習中,我想實現在try-catch語句中實現循環輸入,
即爲當輸入錯誤時,捕獲異常輸出,且可以重新輸入。

第一次編寫的代碼:(此代碼存在bug)

package 異常;

import java.io.Console;
import java.util.Scanner;

import javax.management.RuntimeErrorException;

public class Test4 {
	public static double avg(double x,int y){
		double avg = 0;
		if (y==0) {
			throw new RuntimeException("除數不能爲0!");
			}
		else{
		      avg = x/y;
		}
		     return avg;	
	}

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		while(true){

		try {		
			System.out.println("請輸入總成績:");
			double x = scanner.nextDouble();
			System.out.println("請輸入總人數:");
			int y = scanner.nextInt();
			double avg = avg(x, y);
			System.out.println("平均成績爲:"+avg);		
		} 
		catch (RuntimeException e) {
			System.out.println(e.getMessage());
			continue;
		}
		catch (Exception e) {
			System.out.println("請輸入數字!");
			continue;
		}
		finally {
			scanner.close();			
		}	
		break;
		
		}
	}

}

運行結果:
出現了無限循環
在這裏插入圖片描述

解決方法

bug原因

  • 當輸入異常時,捕捉到異常Exception,開始下一次的循環,但是此時數據緩衝區依舊留有我上次輸入的異常數據,並沒有清除這些數據,這時就又開始捕捉異常Exception,還是錯誤,於是這個異常一直被捕捉一直被輸出。出現了無限循環的bug。
    那麼解決的方法就是將捕捉到的異常Exception數據去清除。

方案一:在catch()語句之中加上scanner.next();
代碼如下:

package 異常;

import java.io.Console;
import java.util.Scanner;

import javax.management.RuntimeErrorException;

public class Test4 {
	public static double avg(double x,int y){
		double avg = 0;
		if (y==0) {
			throw new RuntimeException("除數不能爲0!");
			}
		else{
		      avg = x/y;
		}
		     return avg;	
	}

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		while(true){
		try {		
			System.out.println("請輸入總成績:");
			double x = scanner.nextDouble();
			System.out.println("請輸入總人數:");
			int y = scanner.nextInt();
			double avg = avg(x, y);
			System.out.println("平均成績爲:"+avg);		
		} 
		catch (RuntimeException e) {
			System.out.println(e.getMessage());
			scanner.next();
		}
		catch (Exception e) {
			System.out.println("請輸入數字!");
			scanner.next();
		}			
		}
	}

}

運行結果
成功結束循環

方案二:將Scanner的對象scanner寫在try語句之中
代碼如下:

package 異常;

import java.io.Console;
import java.util.Scanner;

import javax.management.RuntimeErrorException;

public class Test4 {
	public static double avg(double x,int y){
		double avg = 0;
		if (y==0) {
			throw new RuntimeException("除數不能爲0!");
			}
		else{
		      avg = x/y;
		}
		     return avg;	
	}

	public static void main(String[] args) {
		
		
		while(true){
		try {	
			Scanner scanner = new Scanner(System.in);
			System.out.println("請輸入總成績:");
			double x = scanner.nextDouble();
			System.out.println("請輸入總人數:");
			int y = scanner.nextInt();
			double avg = avg(x, y);
			System.out.println("平均成績爲:"+avg);		
		} 
		catch (RuntimeException e) {
			System.out.println(e.getMessage());
		}
		catch (Exception e) {
			System.out.println("請輸入數字!");
		}			
		}
	}

}

運行結果:
在這裏插入圖片描述
成功結束循環!

ps:

try-catch語句一次只能捕獲一個異常

**捕獲或異常爲null(Caught Throwable or Exception is null)
此bug請詳情看此文章(http://www.it1352.com/619288.html)在此不進行解釋

此文章可能有諸多紕漏,如有問題歡迎提出,並在此感謝網上諸位大神的解決方案,本人只是進行一個個人的彙總。

發佈了69 篇原創文章 · 獲贊 20 · 訪問量 1513
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章