【发布时间】:2021-05-06 14:24:08
【问题描述】:
public class TryCatchExample8 {
public static void main(String[] args) {
try
{
int data=50/0; //may throw exception
}
// try to handle the ArithmeticException using ArrayIndexOutOfBoundsException
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
System.out.println("rest of the code");
}
}
我从一个网站得到这个代码。他们使用不同类型的异常类 (ArrayIndexOutOfBoundsException) 处理生成的异常 (Arithmetic Exception)。 它在输出中显示了这一点,
“线程“主”java.lang.ArithmeticException中的异常:/为零 在 TryCatchExample8.main(TryCatchExample8.java:6)"。
如果我们试图捕获 ArrayIndexOutOfBoundsException,它会如何显示?
【问题讨论】:
-
您可以做的第一件事就是尝试一下。这将为您提供一些有用的信息,您可以使用这些信息进行更深入的挖掘。
-
如果您的问题是“这段代码是做什么的”,那么运行它并查看会更快。