异常后继续程序
Continue program after exception
好的,到目前为止我有这个:
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
s 不应该以这种方式使用,但这是所要求的。问题是当我抛出 Exception
时,它不允许我输入新的猜测。有帮助吗?
您应该考虑使用 try catch 块而不是仅仅抛出异常。
try {
//This block throws the exception.
}
Catch(Exception e) {
//This block will run next.
}
你最好检查一下 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.
}
}
像这样?
try {
if (guess < number) {
throw new TooLowException();
} else if (guess > number) {
throw new TooHighException();
} else {
throw new CorrectException();
}
} catch (Exception e) {
e.printStackTrace();
continue;
}
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");
}
}
基本上你有以下问题。
异常会终止程序执行,除非它们被 try catch 捕获(请参阅其他答案)。
因此,要继续,您需要捕获异常并打印一些内容以通知用户结果。
但是如果你这样做,你可以简单地省略异常并首先打印结果。
您正在使用 throw
引发异常,但如果您想处理异常,您应该使用 try
和 catch
块。请参阅此 answer 了解不使用 try
和 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
}
现在你想处理异常而不是冒泡,所以你需要在 try
块之后立即使用 catch
块。
所以,
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
s 时,最好在引发异常时提及异常是什么,例如原因是什么。
为此,覆盖 Exception
class 的 toString()
。如果您在代码中多次使用自定义异常 classes,则不必每次都在 catch
块中向用户写入消息。
编辑:
import java.util.Random;
import java.util.Scanner;
class TooHighException extends Exception {
}
class TooLowException extends Exception {
}
class CorrectException extends Exception {
}
public class HighLow {
/**
* @param args the command line arguments
* @throws TooLowException
* @throws TooHighException
* @throws CorrectException
*/
public static void main(String[] args) {
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();
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);
}
}
} // end main
} // end class
好的,到目前为止我有这个:
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
s 不应该以这种方式使用,但这是所要求的。问题是当我抛出 Exception
时,它不允许我输入新的猜测。有帮助吗?
您应该考虑使用 try catch 块而不是仅仅抛出异常。
try {
//This block throws the exception.
}
Catch(Exception e) {
//This block will run next.
}
你最好检查一下 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.
}
}
像这样?
try {
if (guess < number) {
throw new TooLowException();
} else if (guess > number) {
throw new TooHighException();
} else {
throw new CorrectException();
}
} catch (Exception e) {
e.printStackTrace();
continue;
}
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");
}
}
基本上你有以下问题。
异常会终止程序执行,除非它们被 try catch 捕获(请参阅其他答案)。 因此,要继续,您需要捕获异常并打印一些内容以通知用户结果。 但是如果你这样做,你可以简单地省略异常并首先打印结果。
您正在使用 throw
引发异常,但如果您想处理异常,您应该使用 try
和 catch
块。请参阅此 answer 了解不使用 try
和 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
}
现在你想处理异常而不是冒泡,所以你需要在 try
块之后立即使用 catch
块。
所以,
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
s 时,最好在引发异常时提及异常是什么,例如原因是什么。
为此,覆盖 Exception
class 的 toString()
。如果您在代码中多次使用自定义异常 classes,则不必每次都在 catch
块中向用户写入消息。
编辑:
import java.util.Random;
import java.util.Scanner;
class TooHighException extends Exception {
}
class TooLowException extends Exception {
}
class CorrectException extends Exception {
}
public class HighLow {
/**
* @param args the command line arguments
* @throws TooLowException
* @throws TooHighException
* @throws CorrectException
*/
public static void main(String[] args) {
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();
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);
}
}
} // end main
} // end class