Java Try & Catch 示例
Java Try & Catch example
所以我这里有这段代码,但我不明白为什么我的 Try 语句是 underlined.When 我将鼠标悬停在它上面,它说 b3 已经在方法 main 中定义。我需要做些什么才能让它按照显示的方式工作。 (其他一切都有效)
public class ReviewTest {
public static void main(String[] args)
{
boxes b1 = new boxes();
System.out.printf("My first Box = %s\n", b1.toString());
System.out.printf("My first Box = %s\n", b1);
boxes b2 = new boxes(10.0, 12.4, 13.9);
System.out.printf("My second Box = %s\n" , b2);
System.out.printf("The volumne of b2 = %.3f\n", b2.volume());
System.out.printf("The surface Area of b2 = %.3f\n", b2.surfaceArea());
boxes b3 = null;
try {
boxes b3 = new boxes(-1,4.3, 12);
}
catch (IllegalArgumentException ex)
{
System.out.println(ex.getMessage());
}
}
请更改
boxes b3 = new boxes(-1,4.3, 12);
在
b3 = new boxes(-1,4.3, 12);
这是因为您已经定义了b3
。您需要做的是解决这个问题:
就在 try/catch 块之前,您定义了 boxes b3 = null;
。您需要做的是进入 try
块内部,并删除 b3 = new boxes(-1,4.3, 12);
.
之前的 boxes
部分
所以我这里有这段代码,但我不明白为什么我的 Try 语句是 underlined.When 我将鼠标悬停在它上面,它说 b3 已经在方法 main 中定义。我需要做些什么才能让它按照显示的方式工作。 (其他一切都有效)
public class ReviewTest {
public static void main(String[] args)
{
boxes b1 = new boxes();
System.out.printf("My first Box = %s\n", b1.toString());
System.out.printf("My first Box = %s\n", b1);
boxes b2 = new boxes(10.0, 12.4, 13.9);
System.out.printf("My second Box = %s\n" , b2);
System.out.printf("The volumne of b2 = %.3f\n", b2.volume());
System.out.printf("The surface Area of b2 = %.3f\n", b2.surfaceArea());
boxes b3 = null;
try {
boxes b3 = new boxes(-1,4.3, 12);
}
catch (IllegalArgumentException ex)
{
System.out.println(ex.getMessage());
}
}
请更改
boxes b3 = new boxes(-1,4.3, 12);
在
b3 = new boxes(-1,4.3, 12);
这是因为您已经定义了b3
。您需要做的是解决这个问题:
就在 try/catch 块之前,您定义了 boxes b3 = null;
。您需要做的是进入 try
块内部,并删除 b3 = new boxes(-1,4.3, 12);
.
boxes
部分