【问题标题】:Implementing try-catch block with throw statements使用 throw 语句实现 try-catch 块
【发布时间】:2018-08-14 03:13:35
【问题描述】:

在我必须做的猜谜游戏中,我需要包含一个带有两个 catch 子句的 try-catch 块(一个用于我的两个自定义异常的多 catch 块:BadGuessException 和 TooManyGuessesException,一个用于@987654323 的块@)。

我尝试在我的程序中创建条件来抛出我的自定义异常,因为我不知道它们如何正常工作背后的逻辑。我遇到编译错误,希望帮助我重新编写我的程序,以便它正确实现 try-catch-catch 块。

我的自定义异常类:

public class BadGuessException extends Exception
{
  /**
   * no-arg constructor
   */
  public BadGuessException()
  {
    super("Sorry, that was an invalid guess!");
  }

  /**
   * parametrized constructor
   * @param message String message passed to super class's constructor
   */
  public BadGuessException(String message)
  {
    super(message);
  }
}

public class TooManyGuessesException extends Exception
{
  /**
   * no-arg constructor
   */
  public TooManyGuessesException()
  {
    super("Sorry, too many guesses!");
  }

  /**
   * parametrized constructor
   * @param guess integer value representing amount of guesses (turns)
   */
  public TooManyGuessesException(int guess)
  {
    super("Sorry, you guessed " + guess + " times!");
  }

}

我的程序出现编译错误:

import java.util.Random;
import java.util.*;

public class GuessingGame throws NumberFormatException
{
  public static void main(String[] args)
  {
    //Scanner object to receive user input
    Scanner keyboard = new Scanner(System.in);

    //Create Random class object & random variable
    Random rng = new Random();
    int n = rng.nextInt(10 - 1 + 1) + 1;

    //Initialize incrementor for guessing turns
    int turn = 1;

    //Create variable for user input (guess)
    int guess;

    try
    {
      while(guess != n)
      {
        //Exception handling for more than five turns
        if(turn > 5)
          throw new TooManyGuessesException();

        //Prompt user to enter their guess
        System.out.println("Guess a number between 1 and 10 inclusive.");
        System.out.println("Hint: the answer is " + n);
        //Receive user input (their guess)
        guess = keyboard.nextInt();

        //Increment turn variable
        turn++;

        if(guess < 1 || guess > 10)
          throw new BadGuessException();
        else if(guess == n)
          System.out.println("YOU WIN!\nIt took you " + turn + " attempts.");
      }
    }
    catch(BadGuessException e | TooManyGuessesException e)
    {
      e.getMessage();
    }
    catch(NumberFormatException e)
    {
      System.out.println("Sorry, you entered an invalid number format.");
    }

  }
}

【问题讨论】:

  • 你得到什么编译错误?不要让我们猜测。
  • Error: Syntax error on tokens, AnnotationName expected instead,以及由于我的代码结构中的错误(我具体不知道)而导致的看起来像语法错误的其他编译错误。

标签: java exception-handling try-catch throw


【解决方案1】:

在 GuessingGame 类中进行更改 Remove e after BadGuessException in multiple try block。并用 0 初始化guess 并从你的类声明中删除 NumberformatException;

import java.util.Random;
import java.util.*;

public class GuessingGame
{
  public static void main(String[] args)
  {
    //Scanner object to receive user input
    Scanner keyboard = new Scanner(System.in);

    //Create Random class object & random variable
    Random rng = new Random();
    int n = rng.nextInt(10 - 1 + 1) + 1;

    //Initialize incrementor for guessing turns
    int turn = 1;

    //Create variable for user input (guess)
    int guess = 0 ;

    try
    {
      while(guess != n)
      {
        //Exception handling for more than five turns
        if(turn > 5)
          throw new TooManyGuessesException();

        //Prompt user to enter their guess
        System.out.println("Guess a number between 1 and 10 inclusive.");
        System.out.println("Hint: the answer is " + n);
        //Receive user input (their guess)
        guess = keyboard.nextInt();

        //Increment turn variable
        turn++;

        if(guess < 1 || guess > 10)
          throw new BadGuessException();
        else if(guess == n)
          System.out.println("YOU WIN!\nIt took you " + turn + " attempts.");
      }
    }
    catch(BadGuessException | TooManyGuessesException e)
    {
      e.getMessage();
    }
    catch(NumberFormatException e)
    {
      System.out.println("Sorry, you entered an invalid number format.");
    }

  }
}

【讨论】:

  • 作为序言,我还删除了标题中的throws 语句。当我尝试这个时,我得到:Error: Multi-catch parameters are not allowed for source level below 1.7
  • 您使用的是哪个 JDK 版本的 java 以及您是如何编译代码的
  • 我在 DrJava 上编译,我想我正在使用 JDK10
  • 您使用的是低于 1.7 版本的 JDK,JDK 1.7 中引入了多个 try-catch 功能
  • 通过编辑 > 首选项 > 编译器选项并单击无首选项组合框来检查您的 JDK 版本
猜你喜欢
  • 2019-06-25
  • 2015-09-08
  • 2010-12-14
  • 2014-02-03
  • 2010-09-24
  • 1970-01-01
  • 2011-11-29
  • 2013-10-07
  • 1970-01-01
相关资源
最近更新 更多