理解Java异常处理:简单的Try-Catch块
我正在尝试学习Java异常处理。我编写了以下代码:理解Java异常处理:简单的Try-Catch块,java,exception-handling,Java,Exception Handling,我正在尝试学习Java异常处理。我编写了以下代码: import java.util.*; public class Problem1 { static Scanner length = new Scanner(System.in); public static void main(String[] args) { double x; double y; System.out.println("In
import java.util.*;
public class Problem1
{
static Scanner length = new Scanner(System.in);
public static void main(String[] args)
{
double x;
double y;
System.out.println("Input a length in Feet and than Inches.");
x = length.nextDouble();
y = length.nextDouble();
try
{
System.out.println("Feet - Inches:" + x * 12);
System.out.println("Inches - Centimeters:" + y * 2.14);
}
catch (InputMismatchException imeRef)
{
System.out.println("Do not use letters" + imeRef.toString());
}
}
}
该程序只是将以英尺和英寸为单位的输入转换为英寸。我试图通过输入以下内容来打破它:
-1
e
程序中断,但我没有正确捕获和处理异常。知道我做错了什么吗
谢谢您需要将读取用户输入的两条输入语句移动到
try
块中。只有在读取输入时才会抛出错误,要在catch中处理该错误,这些语句需要在try
块中,因为catch将只处理在其相应的try
块中抛出的异常
try {
x = length.nextDouble(); // moved inside try
y = length.nextDouble(); // moved inside try
System.out.println("Feet - Inches:" + x * 12);
System.out.println("Inches - Centimeters:" + y * 2.14);
}
您需要将从用户读取输入的两条输入语句移动到
try
块中。只有在读取输入时才会抛出错误,要在catch中处理该错误,这些语句需要在try
块中,因为catch将只处理在其相应的try
块中抛出的异常
try {
x = length.nextDouble(); // moved inside try
y = length.nextDouble(); // moved inside try
System.out.println("Feet - Inches:" + x * 12);
System.out.println("Inches - Centimeters:" + y * 2.14);
}
将以下两行放在一起:
x = length.nextDouble();
y = length.nextDouble();
在块中尝试块-
try {
x = length.nextDouble();
y = length.nextDouble();
System.out.println("Feet - Inches:" + x * 12);
System.out.println("Inches - Centimeters:" + y * 2.14);
}
catch (InputMismatchException imeRef) {
System.out.println("Do not use letters" + imeRef.toString());
}
catch
块仅捕获其相应的try
块内的语句引发的匹配异常。放置以下两行-
try {
x = length.nextDouble(); // moved inside try
y = length.nextDouble(); // moved inside try
System.out.println("Feet - Inches:" + x * 12);
System.out.println("Inches - Centimeters:" + y * 2.14);
}
x = length.nextDouble();
y = length.nextDouble();
在块中尝试块-
try {
x = length.nextDouble();
y = length.nextDouble();
System.out.println("Feet - Inches:" + x * 12);
System.out.println("Inches - Centimeters:" + y * 2.14);
}
catch (InputMismatchException imeRef) {
System.out.println("Do not use letters" + imeRef.toString());
}
catch
块仅捕获其对应的try
块中的语句引发的匹配异常。无法捕获异常是因为在调用变量x和y的length.nextDouble()时引发异常
try {
x = length.nextDouble(); // moved inside try
y = length.nextDouble(); // moved inside try
System.out.println("Feet - Inches:" + x * 12);
System.out.println("Inches - Centimeters:" + y * 2.14);
}
x = length.nextDouble();
y = length.nextDouble();
但是您没有将它们放入try-catch代码中。将上面两行代码放到try-catch,您将捕获异常。您无法捕获异常是因为在调用变量x和y的length.nextDouble()时引发了异常
x = length.nextDouble();
y = length.nextDouble();
但是您没有将它们放入try-catch代码中。将上面的两行代码放到try-catch,您将捕获异常。catch语句只捕获在其相应的try块中抛出的异常
这就是你想要的。这会提示用户每次提出一个问题,并重置并再次询问输入错误的问题。请注意catch中的length.next(),这是避免无限循环所必需的,您必须前进超过该错误标记
while (true) {
try {
System.out.println("Input a length in feet");
double x = length.nextDouble();
System.out.println("Input a length in inches");
double y = length.nextDouble();
System.out.println("Feet - Inches:" + x * 12);
System.out.println("Inches - Centimeters:" + y * 2.14);
break;
}
catch (InputMismatchException imeRef) {
System.out.println("Do not use letters" + imeRef.toString());
// need to purge the bad token
length.next();
}
}
catch语句只捕获在其相应的try块中抛出的异常
这就是你想要的。这会提示用户每次提出一个问题,并重置并再次询问输入错误的问题。请注意catch中的length.next(),这是避免无限循环所必需的,您必须前进超过该错误标记
while (true) {
try {
System.out.println("Input a length in feet");
double x = length.nextDouble();
System.out.println("Input a length in inches");
double y = length.nextDouble();
System.out.println("Feet - Inches:" + x * 12);
System.out.println("Inches - Centimeters:" + y * 2.14);
break;
}
catch (InputMismatchException imeRef) {
System.out.println("Do not use letters" + imeRef.toString());
// need to purge the bad token
length.next();
}
}
您希望在将用户输入转换为双精度输入时捕获任何异常。这在下面的代码行中发生:x=length.nextDouble()
y=length.nextDouble()代码>因此这两行应该在try语句中Yes!我盯着这个看了这么久,错过了显而易见的东西。非常感谢。如果评论有用,请投票表决。感谢您的回答由于某些原因不允许我向上投票。我还需要获得它,以便它不允许输入负数,我不确定在另一个catch语句中必须使用哪种类型的异常。您希望在将用户输入转换为double时捕获任何异常。这在下面的代码行中发生:x=length.nextDouble()
y=length.nextDouble()代码>因此这两行应该在try语句中Yes!我盯着这个看了这么久,错过了显而易见的东西。非常感谢。如果评论有用,请投票表决。谢谢你的回答因为某种原因不允许我投票。我还需要得到它,这样它就不允许输入负数,我不确定在另一个catch语句中必须使用哪种类型的异常。