Java 异常,捕获后它仍然打印之后的内容
Java exceptions, after catch it stills prints whats after
我正在使用一些异常,但即使抛出并捕获了一个异常,它也会在 catch 块之后继续输出什么。
我希望抛出的异常被捕获并只打印出捕获主体中的内容,除非没有异常并继续进行最后一个 souf。
但不知何故,当我有异常时,我的 catch 主体被打印了,但它后面的 souf 也不应该被打印。
如何组织这些例外情况?
-------- 抛出异常的方法
public double getHeight() throws ExceptionCheck {
//if end point is not set, return -1 (error)
if(points[1] == null){
throw new ExceptionCheck("The height cannot be calculated, the end point is missing!\n\n");
} else {
double height = points[1].getY() - points[0].getY();
return height;
}
}
-------- 处理getHeight抛出的方法
@Override
public double getArea() {
//if end point is not set, return -1 (error)
double area = 0;
try {
area = getHeight() * getWidth();
}
catch(ExceptionCheck e){
System.out.printf("The area cannot be calculated, the end point is missing!\n\n");
}
return area;
}
--------- 这里不应打印捕获后的最后一个 SOUF,但无论如何都会打印
private static void printArea(Shape shape) {
System.out.println("Printing area of a " + shape.getClass().getSimpleName());
double area = 0d;
// Get area of the shape and print it.
try {
area = shape.getArea();
}
catch(ExceptionCheck e){
System.out.printf(e.getMessage());
}
System.out.println("The area is: " + area);
}
那不是 catch
的工作方式。如果出现异常时不应打印该内容,则您必须 将其移动到try
的正文中。喜欢,
// Get area of the shape and print it.
try {
double area = shape.getArea();
System.out.println("The area is: " + area); // <-- if the previous line throws
// an exception, this will not print.
}
catch(ExceptionCheck e){
System.out.printf(e.getMessage());
}
您的方法 getArea
实际上 throw
并不 Exception
。它打印并吞下它。要调用上面的 catch
,您还必须将 getArea
修改为
@Override
public double getArea() throws ExceptionCheck {
try {
return getHeight() * getWidth();
}
catch(ExceptionCheck e){
System.out.printf("The area cannot be calculated, the end point is missing!\n\n");
throw e; // <-- add this.
}
}
我正在使用一些异常,但即使抛出并捕获了一个异常,它也会在 catch 块之后继续输出什么。
我希望抛出的异常被捕获并只打印出捕获主体中的内容,除非没有异常并继续进行最后一个 souf。
但不知何故,当我有异常时,我的 catch 主体被打印了,但它后面的 souf 也不应该被打印。
如何组织这些例外情况?
-------- 抛出异常的方法
public double getHeight() throws ExceptionCheck {
//if end point is not set, return -1 (error)
if(points[1] == null){
throw new ExceptionCheck("The height cannot be calculated, the end point is missing!\n\n");
} else {
double height = points[1].getY() - points[0].getY();
return height;
}
}
-------- 处理getHeight抛出的方法
@Override
public double getArea() {
//if end point is not set, return -1 (error)
double area = 0;
try {
area = getHeight() * getWidth();
}
catch(ExceptionCheck e){
System.out.printf("The area cannot be calculated, the end point is missing!\n\n");
}
return area;
}
--------- 这里不应打印捕获后的最后一个 SOUF,但无论如何都会打印
private static void printArea(Shape shape) {
System.out.println("Printing area of a " + shape.getClass().getSimpleName());
double area = 0d;
// Get area of the shape and print it.
try {
area = shape.getArea();
}
catch(ExceptionCheck e){
System.out.printf(e.getMessage());
}
System.out.println("The area is: " + area);
}
那不是 catch
的工作方式。如果出现异常时不应打印该内容,则您必须 将其移动到try
的正文中。喜欢,
// Get area of the shape and print it.
try {
double area = shape.getArea();
System.out.println("The area is: " + area); // <-- if the previous line throws
// an exception, this will not print.
}
catch(ExceptionCheck e){
System.out.printf(e.getMessage());
}
您的方法 getArea
实际上 throw
并不 Exception
。它打印并吞下它。要调用上面的 catch
,您还必须将 getArea
修改为
@Override
public double getArea() throws ExceptionCheck {
try {
return getHeight() * getWidth();
}
catch(ExceptionCheck e){
System.out.printf("The area cannot be calculated, the end point is missing!\n\n");
throw e; // <-- add this.
}
}