是否可以从两种不同的方法中获得两个具有相同异常类型的捕获?
Is it possible to have two catches with the same Exception type from two different methods?
我是新来的编程新手,如果这太长了,我很抱歉。我确信有更简单的方法可以做到这一点,但我真的很想看看这是否可以得到解决。
我正在开发一个程序,该程序接受用户输入(双精度)并从摄氏度转换为华氏度 (cToF),反之亦然 (fToC),约束范围为 0 到 1000。我对 cToF 和 fToC 都有一个单独的方法。
当用户没有输入双精度时,我想抛出一个异常。我在 main 中有 catch (InputMismatchException cToF) 和 catch (InputMismatchException2 fToC) 做不同的事情。
另外,我怀疑 fToC 的数学计算不正确,我只是还没弄明白。
我尝试将 InputMismatchException2 的名称更改为 InputMismatchException,但我得到 "exception has already been caught"。
我已经导入 java.util.*。
我尝试在 main 方法和两种转换方法的开头添加 "throws Exception" 。
我不确定 try-catch 是否应该在主要方法、个别方法或两者中。
public static void main(String args[]) throws InputMismatchException, InputMismatchException2 {
Scanner choice = new Scanner(System.in);
String unit = "n";
boolean typo = false;
double tempC = 0;
double tempF = 0;
try {
start();//method to determine conversion type (F-C or C-F)
if (typo == false) {
do {//forever loop
question(unit);
}//end do loop
while (typo == false);
}//end if
System.out.println("Thank you, goodbye!");
}//end try
catch (InputMismatchException cToFE) {//if a non-double is entered
System.out.println("The value " + tempC + " is not within previously defined constraints.");
System.out.println("Please enter a new value.");
cToF(unit);
}//end cToF catch
catch (InputMismatchException2 fToCE) {//if a non-double is entered
System.out.println("The value " + tempF + " is not within previously defined constraints.");
System.out.println("Please enter a new value.");
fToC(unit);
}//end fToC catch
}//end main method
public static void cToF(String unit) throws InputMismatchException {
System.out.println();
System.out.println("Please enter a value from 0 to 1000.");//CONSTRAINTS: 0 <= temp <= 1000
Scanner temp = new Scanner(System.in);
double tempC = temp.nextDouble();
if (0 <= tempC && tempC <= 1000) {//if tempC is within constraints
double tempF = (tempC * 1.8) + 32;//arithmetic with tempC == tempF
System.out.println(tempC + " degrees Celsius is " + tempF + " degrees Fahrenheit");//print conversion
}//end if
else if (tempC < 0 || tempC > 1000) {//if tempC is NOT within constraints
System.out.println("The value " + tempC + " is not within previously defined constraints.");
System.out.println("Please enter a new value.");
cToF(unit);
}//end else if
else {//if non-double entered
//InputMismatchException cToFE = new InputMismatchException();
throw new InputMismatchException();
}//end else
}//end cToF method
public static void fToC(String unit) throws InputMismatchException2 {
System.out.println();
System.out.println("Please enter a value from 0 to 1000.");//CONSTRAINTS: 0 <= temp <= 1000
Scanner temp = new Scanner(System.in);
double tempF = temp.nextDouble();
if (0 <= tempF && tempF <= 1000) {//if tempF is within constraints
double tempC = (32 - tempF) / 1.8;//arithmetic with tempF == tempC
System.out.println(tempF + " degrees Fahrenheit is " + tempC + " degrees Celsius");//print conversion
}//end if
else if (tempF < 0 || tempF > 1000) {//if tempF is NOT within constraints
System.out.println("The value " + tempF + " is not within previously defined constraints.");
System.out.println("Please enter a new value.");
fToC(unit);
}//end else if
else {//if non-double entered
//InputMismatchException2 fToCE = new InputMismatchException2();
throw new InputMismatchException2();
}//end else
}//end fToC method
我想要的:
如果用户在任一方法 (fToC/cToF) 中输入字母,则打印 The value [tempF/tempC respectively] 不在先前定义的限制范围内。请输入新值。
然后递归调用任一方法以重新启动临时输入过程。
我得到了什么(使用当前代码):
$ javac Temperature.java
Temperature.java:5: error: cannot find symbol
public static void main(String args[]) throws InputMismatchException, InputMismatchException2 {
^
symbol: class InputMismatchException2
location: class Temperature
Temperature.java:125: error: cannot find symbol
public static void fToC(String unit) throws InputMismatchException2 {
^
symbol: class InputMismatchException2
location: class Temperature
Temperature.java:42: error: cannot find symbol
catch (InputMismatchException2 fToCE) {//if a non-double is entered
^
symbol: class InputMismatchException2
location: class Temperature
Temperature.java:151: error: cannot find symbol
throw new InputMismatchException2();
^
symbol: class InputMismatchException2
location: class Temperature
4 errors
简而言之,,你不能这样做。否则,将无法决定在抛出异常时执行正确的 catch 语句。
你这里的错误是 "error: cannot find symbol"。根据错误跟踪,这意味着找不到符号 InputMismatchException2
。本质上,如果你想抛出一个 InputMismatchException2
,你将需要定义一个单独的 class InputMismatchException2
(扩展 InputMismatchException
)。然后你可以 运行 你的代码的原因是你实际上会抛出和捕捉两种不同的 InputMismatchException
, InputMismatchException
和 InputMismatchException2
.
但是!初始化 InputMismatchException
with a message, and using its getMessage 方法以在 catch 语句中提取相关信息可能会好得多。
仔细查看代码,generally not recommended 将异常用作控制流的一部分。
我是新来的编程新手,如果这太长了,我很抱歉。我确信有更简单的方法可以做到这一点,但我真的很想看看这是否可以得到解决。
我正在开发一个程序,该程序接受用户输入(双精度)并从摄氏度转换为华氏度 (cToF),反之亦然 (fToC),约束范围为 0 到 1000。我对 cToF 和 fToC 都有一个单独的方法。
当用户没有输入双精度时,我想抛出一个异常。我在 main 中有 catch (InputMismatchException cToF) 和 catch (InputMismatchException2 fToC) 做不同的事情。
另外,我怀疑 fToC 的数学计算不正确,我只是还没弄明白。
我尝试将 InputMismatchException2 的名称更改为 InputMismatchException,但我得到 "exception has already been caught"。
我已经导入 java.util.*。
我尝试在 main 方法和两种转换方法的开头添加 "throws Exception" 。
我不确定 try-catch 是否应该在主要方法、个别方法或两者中。
public static void main(String args[]) throws InputMismatchException, InputMismatchException2 {
Scanner choice = new Scanner(System.in);
String unit = "n";
boolean typo = false;
double tempC = 0;
double tempF = 0;
try {
start();//method to determine conversion type (F-C or C-F)
if (typo == false) {
do {//forever loop
question(unit);
}//end do loop
while (typo == false);
}//end if
System.out.println("Thank you, goodbye!");
}//end try
catch (InputMismatchException cToFE) {//if a non-double is entered
System.out.println("The value " + tempC + " is not within previously defined constraints.");
System.out.println("Please enter a new value.");
cToF(unit);
}//end cToF catch
catch (InputMismatchException2 fToCE) {//if a non-double is entered
System.out.println("The value " + tempF + " is not within previously defined constraints.");
System.out.println("Please enter a new value.");
fToC(unit);
}//end fToC catch
}//end main method
public static void cToF(String unit) throws InputMismatchException {
System.out.println();
System.out.println("Please enter a value from 0 to 1000.");//CONSTRAINTS: 0 <= temp <= 1000
Scanner temp = new Scanner(System.in);
double tempC = temp.nextDouble();
if (0 <= tempC && tempC <= 1000) {//if tempC is within constraints
double tempF = (tempC * 1.8) + 32;//arithmetic with tempC == tempF
System.out.println(tempC + " degrees Celsius is " + tempF + " degrees Fahrenheit");//print conversion
}//end if
else if (tempC < 0 || tempC > 1000) {//if tempC is NOT within constraints
System.out.println("The value " + tempC + " is not within previously defined constraints.");
System.out.println("Please enter a new value.");
cToF(unit);
}//end else if
else {//if non-double entered
//InputMismatchException cToFE = new InputMismatchException();
throw new InputMismatchException();
}//end else
}//end cToF method
public static void fToC(String unit) throws InputMismatchException2 {
System.out.println();
System.out.println("Please enter a value from 0 to 1000.");//CONSTRAINTS: 0 <= temp <= 1000
Scanner temp = new Scanner(System.in);
double tempF = temp.nextDouble();
if (0 <= tempF && tempF <= 1000) {//if tempF is within constraints
double tempC = (32 - tempF) / 1.8;//arithmetic with tempF == tempC
System.out.println(tempF + " degrees Fahrenheit is " + tempC + " degrees Celsius");//print conversion
}//end if
else if (tempF < 0 || tempF > 1000) {//if tempF is NOT within constraints
System.out.println("The value " + tempF + " is not within previously defined constraints.");
System.out.println("Please enter a new value.");
fToC(unit);
}//end else if
else {//if non-double entered
//InputMismatchException2 fToCE = new InputMismatchException2();
throw new InputMismatchException2();
}//end else
}//end fToC method
我想要的:
如果用户在任一方法 (fToC/cToF) 中输入字母,则打印 The value [tempF/tempC respectively] 不在先前定义的限制范围内。请输入新值。
然后递归调用任一方法以重新启动临时输入过程。
我得到了什么(使用当前代码):
$ javac Temperature.java
Temperature.java:5: error: cannot find symbol
public static void main(String args[]) throws InputMismatchException, InputMismatchException2 {
^
symbol: class InputMismatchException2
location: class Temperature
Temperature.java:125: error: cannot find symbol
public static void fToC(String unit) throws InputMismatchException2 {
^
symbol: class InputMismatchException2
location: class Temperature
Temperature.java:42: error: cannot find symbol
catch (InputMismatchException2 fToCE) {//if a non-double is entered
^
symbol: class InputMismatchException2
location: class Temperature
Temperature.java:151: error: cannot find symbol
throw new InputMismatchException2();
^
symbol: class InputMismatchException2
location: class Temperature
4 errors
简而言之,
你这里的错误是 "error: cannot find symbol"。根据错误跟踪,这意味着找不到符号 InputMismatchException2
。本质上,如果你想抛出一个 InputMismatchException2
,你将需要定义一个单独的 class InputMismatchException2
(扩展 InputMismatchException
)。然后你可以 运行 你的代码的原因是你实际上会抛出和捕捉两种不同的 InputMismatchException
, InputMismatchException
和 InputMismatchException2
.
但是!初始化 InputMismatchException
with a message, and using its getMessage 方法以在 catch 语句中提取相关信息可能会好得多。
仔细查看代码,generally not recommended 将异常用作控制流的一部分。