想知道是否有人可以告诉我为什么我的try-catch不起作用.当我输入一个整数时,我输入一个字符,程序只是以异常退出.它与我在另一个程序中编写的try-catch相同.不知道为什么它不起作用.:(
import java.io.*;
import java.util.*;
public class Lab8FileIO {
public static void main(String[] args) throws IOException {
int menuChoice = 0;
String filename, userInput, choice;
boolean playAgain = true;
char response;
Scanner keyboard = new Scanner(System.in);
while (playAgain)
{
// Display menu and get user selection
displayMenu();
do
{
try
{
menuChoice = keyboard.nextInt();
if ((menuChoice < 1) || (menuChoice > 4)) // Make sure user enters a number within the specified range
System.out.print("You must enter a number from 1 …Run Code Online (Sandbox Code Playgroud) 我正在测试抛出两个不同异常的方法.这是我的标题:
@Test (expected = A8InvalidInputException.class)
public void testGuessCharacter() throws A8InvalidInputException, A8AlreadyGuessedException
{
...
}
正文有两个try/catch块(在SO上搜索会产生一个帖子,说明你是如何测试抛出异常的),每个例外都有一个.在我看来,我应该将其分解为两种测试方法,特别是因为我只能有一个预期的属性.但是,当我这样做时,应该测试A8InvalidInputException的方法需要针对A8AlreadyGuessedException的try/catch,并且应该测试A8AlreadyGuessedException的方法需要针对A8InvalidInputException的try/catch.我不太确定如何编写这个测试.这是我试图测试的方法:
/**
* This method returns whether a specified character exists in the keyPhrase field
* @param guess a character being checked for in the keyPhrase field
* @return returns whether a specified character exists in the keyPhrase field
* @throws A8InvalidInputException if a non-valid character is passed as input to this method
* @throws A8AlreadyGuessedException if a valid character which has already been …Run Code Online (Sandbox Code Playgroud)