我的问题是关于简单除零示例中的 try-catch 块。你看到第一行尝试了吗?如果我将这两个变量中的任何一个转换为双精度,程序将无法识别 catch 块。在我看来,无论我是否强制转换,都必须执行 catch 块。这段代码有什么问题?
public static void main(String[] args) {
int pay=8,payda=0;
try {
double result=pay/(double)payda; // if I cast any of the two variables, program does not recognize the catch block, why is it so?
System.out.println(result);
System.out.println("inside-try");
} catch (Exception e) {
System.out.println("division by zero exception");
System.out.println("inside-catch");
}
}