try catch catch中没有retrun
时间: 2024-03-26 09:31:17 浏览: 55
c语言没有try catch的替代方案
在try-catch语句中,如果catch块中没有return语句,那么程序会继续执行finally块中的代码。无论是否有异常抛出,finally块中的代码都会被执行。以下是一个示例:
```java
public class TryCatchExample {
public static void main(String[] args) {
try {
int result = divide(10, 0);
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Caught exception: " + e.getMessage());
} finally {
System.out.println("Finally block executed");
}
}
public static int divide(int num1, int num2) {
try {
return num1 / num2;
} catch (ArithmeticException e) {
System.out.println("Caught exception in divide method: " + e.getMessage());
}
// 如果catch块中没有return语句,会继续执行finally块中的代码
return 0;
}
}
```
输出结果:
```
Caught exception in divide method: / by zero
Finally block executed
Result: 0
```
阅读全文