异常后继续编程

时间:2017-03-13 23:18:26

标签: java exception continue

好的,所以我到目前为止:

import java.util.Random;
import java.util.Scanner;
class TooHighException extends Exception {}

class TooLowException extends Exception {}

class CorrectException extends Exception {}

public class HighLow {     
    public static void main(String[] args) throws TooLowException, TooHighException, CorrectException {

        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        int number = random.nextInt(100);
        int guess = -1;
        while (guess != number) {
            System.out.print("Enter your guess: ");
            guess = scanner.nextInt();
            if (guess < number) {
                throw new TooLowException();
            } else if (guess > number) {
                throw new TooHighException();
            } else {
                throw new CorrectException();
            }
        }
    }
}

应该发生的是程序选择一个随机数,并提示用户猜测该数字。然后它将说明猜测是否太高,太低或正确。我知道Exception不应该以这种方式使用,但它正是被要求的。问题是,当我得到Exception时,它不允许我输入新的猜测。有什么帮助吗?

6 个答案:

答案 0 :(得分:1)

你最好检查一下try / catch。

while(guess!=number){
try{
        System.out.print("Enter your guess: ");
           guess = scanner.nextInt();
           if (guess < number) {
               throw new TooLowException();
           } else if (guess > number) {
               throw new TooHighException();
           } else {
               throw new CorrectException();
           }
    }



   catch(TooLowException ex){
System.out.println("Too low.");   
}
catch(TooHighException ex){
System.out.println("Too high.");   
}
catch(CorrectException ex){
System.out.println("Correct.");   
}
   finally{
       //This block always executes.
   }
}

答案 1 :(得分:0)

您应该使用try catch块而不是仅仅抛出异常。

File.WriteAllLines("Text);

答案 2 :(得分:0)

喜欢这个吗?

try {
    if (guess < number) {
        throw new TooLowException(); 
    } else if (guess > number) {
        throw new TooHighException(); 
    } else { 
        throw new CorrectException(); 
    }
} catch (Exception e) {
    e.printStackTrace();
    continue;
}  

答案 3 :(得分:0)

while (guess != number) {
    System.out.print("Enter your guess: ");
    guess = scanner.nextInt();
 try{ 
  if (guess < number) {
        throw new TooLowException();// when this line executes exception is thrown, anything written below this line will not run. If there is a catch block to catch this exception below try block, that will run next
    } else if (guess > number) {
        throw new TooHighException();
    } else {
        throw new CorrectException();
    }
 }
 catch(TooLowException e){
    //this will catch the exception you throw.
    //Usually we write e.printStacktrace() to get to know where the error occurred 
   // we also deallocate the allocated resources.
   //but in your case
   System.out.println("Too low");

 }
  catch(TooHighException e){

   System.out.println("Too high");

 }
  catch(CorrectException e){

   System.out.println("Correct");

 }
}

答案 4 :(得分:0)

基本上你有以下问题。

异常终止程序执行,除非它们被try catch捕获(参见其他答案)。 因此,要继续,您需要捕获异常并打印一些内容以通知用户结果。 但是如果你这样做,你可以简单地省略异常并首先打印结果。

答案 5 :(得分:0)

您正在使用article引发异常,但如果您要处理异常,则应使用throwtry阻止。请参阅此answer,了解当您不使用catchtry阻止时会发生什么。

在可能发生异常的地方使用catch阻止。

在您的代码中,它发生在try块中。

所以,

if-else if-else

现在您要处理异常而不是冒泡,因此您需要在while (guess != number) { System.out.print("Enter your guess: "); guess = scanner.nextInt(); try { if (guess < number) { throw new TooLowException(); } else if (guess > number) { throw new TooHighException(); } else { throw new CorrectException(); } } // end the try block } 阻止后立即使用catch阻止。

所以,

try

当您使用由您创建的while (guess != number) { System.out.print("Enter your guess: "); guess = scanner.nextInt(); try { if (guess < number) { throw new TooLowException(); } else if (guess > number) { throw new TooHighException(); } else { throw new CorrectException(); } } catch(Exception e) { System.out.println(e); } } 时,提及异常是什么是一件好事,就像引发异常时的原因一样。

为此,请覆盖Exception课程的toString()。如果您在代码中多次使用自定义异常类,则不必每次都在Exception块中将消息写入用户。

编辑:

catch