如何循环 Try 块直到没有捕获到异常?
How can I loop the Try block until no exceptions are caught?
如何循环遍历 Try-Catch 的 Try 块部分,直到没有捕获到异常?
例如;您将分母输入为 0(除以 0),捕获并处理 ArithmeticException,并且 Try 块再次为 运行(要求您再次输入分子和分母)。当没有捕获到任何东西时,程序停止。
import java.util.InputMismatchException;
import java.util.Scanner;
public class Activity {
public static void main(String[] args) {
Fraction sampleFraction = new Fraction(8,3);
Scanner scan = new Scanner(System.in);
try {
System.out.print ("Enter the numerator: ");
sampleFraction.setNumerator(scan.nextInt());
System.out.print ("Enter the denominator: ");
sampleFraction.setDenominator(scan.nextInt());
System.out.println ("The fraction " + sampleFraction.getNumerator() + "/" +
sampleFraction.getDenominator() + " is equal to " + sampleFraction.toMixedNumber());
}
catch (ArithmeticException e) {
System.out.println("You can't divide by zero! \n");
}
catch (InputMismatchException e) {
System.out.println("Please enter whole numbers (Integers) only! \n");
}
catch (Exception e) {
System.out.println("An unexpected exception was caught; please try again! \n");
}
scan.close();
}
}
这只是 driver/main class。如果需要,我可以提供分数 class,尽管它只处理所有计算和打印数学的方法。
用循环包围 try/catch,并在 try 块的末尾中断:
while (true) {
try {
// Do things that might throw exceptions...
// This breaks out of the `while` loop.
break;
} catch (ArithmeticException e) {
// ...
} catch (InputMismatchException e) {
// ...
} /* etc */
// Unless we break/return in the catch blocks, the loop will execute again.
}
话虽如此,最好首先避免异常。例外是为了处理异常的事情;如果你知道是什么原因导致抛出异常,你也许可以避免它。
例如,当您尝试除以零时会出现 ArithmeticException
:与其依赖捕获异常,不如先检查分母不为零:
int denominator = scan.nextInt();
if (denominator == 0) {
System.out.println("You can't divide by zero! \n");
continue;
}
sampleFraction.setDenominator(denominator);
如何循环遍历 Try-Catch 的 Try 块部分,直到没有捕获到异常?
例如;您将分母输入为 0(除以 0),捕获并处理 ArithmeticException,并且 Try 块再次为 运行(要求您再次输入分子和分母)。当没有捕获到任何东西时,程序停止。
import java.util.InputMismatchException;
import java.util.Scanner;
public class Activity {
public static void main(String[] args) {
Fraction sampleFraction = new Fraction(8,3);
Scanner scan = new Scanner(System.in);
try {
System.out.print ("Enter the numerator: ");
sampleFraction.setNumerator(scan.nextInt());
System.out.print ("Enter the denominator: ");
sampleFraction.setDenominator(scan.nextInt());
System.out.println ("The fraction " + sampleFraction.getNumerator() + "/" +
sampleFraction.getDenominator() + " is equal to " + sampleFraction.toMixedNumber());
}
catch (ArithmeticException e) {
System.out.println("You can't divide by zero! \n");
}
catch (InputMismatchException e) {
System.out.println("Please enter whole numbers (Integers) only! \n");
}
catch (Exception e) {
System.out.println("An unexpected exception was caught; please try again! \n");
}
scan.close();
}
}
这只是 driver/main class。如果需要,我可以提供分数 class,尽管它只处理所有计算和打印数学的方法。
用循环包围 try/catch,并在 try 块的末尾中断:
while (true) {
try {
// Do things that might throw exceptions...
// This breaks out of the `while` loop.
break;
} catch (ArithmeticException e) {
// ...
} catch (InputMismatchException e) {
// ...
} /* etc */
// Unless we break/return in the catch blocks, the loop will execute again.
}
话虽如此,最好首先避免异常。例外是为了处理异常的事情;如果你知道是什么原因导致抛出异常,你也许可以避免它。
例如,当您尝试除以零时会出现 ArithmeticException
:与其依赖捕获异常,不如先检查分母不为零:
int denominator = scan.nextInt();
if (denominator == 0) {
System.out.println("You can't divide by zero! \n");
continue;
}
sampleFraction.setDenominator(denominator);