Java 异常处理优先级
我正在准备一个包括异常处理的期中考试,我被以下代码输出的原因难倒了:这里2,这里我在,这里a。看起来他们试图访问在try块中创建的数组列表中的有效点,为什么它会抛出indexoutofbounds异常Java 异常处理优先级,java,Java,我正在准备一个包括异常处理的期中考试,我被以下代码输出的原因难倒了:这里2,这里我在,这里a。看起来他们试图访问在try块中创建的数组列表中的有效点,为什么它会抛出indexoutofbounds异常 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class MExceptionMult
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class MExceptionMulti {
public void method1 ()
{
try {
ArrayList l = new ArrayList(10);
l.get(1);
}
catch (NullPointerException e) {
System.out.println ("here 1");
}
catch (Exception e) {
System.out.println ("here 2");
throw new IndexOutOfBoundsException();
}
finally {
System.out.println ("here I am");
}
System.out.println ("here 4");
}
public static void main(String[] args) throws IOException {
MExceptionMulti example = new MExceptionMulti();
try {
example.method1();
example.method2();
System.out.println ("I am after the method");
}
catch (IndexOutOfBoundsException e) {
System.out.println ("here A");
throw new IllegalArgumentException();
}
// catch (Exception e) {
// System.out.println ("here B1");
// }
// catch (Exception e) {
// System.out.println ("here B2");
// }
// catch (Throwable e) {
// System.out.println ("here B3");
// }
System.out.println ("Here at the end of it all!!!");
}
public void method2 () throws IOException
{
BufferedReader in = new BufferedReader (new
InputStreamReader(System.in));
System.out.println ("How much do you weight?");
String inputit="";
inputit = in.readLine();
int weight = Integer.parseInt(inputit);
}
}
编辑代码
结果:
这里1
我来了
这里4
你体重多少
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class MExceptionMulti {
public void method1 ()
{
try {
ArrayList l = new ArrayList(10);
l.get(1);
}
catch (IndexOutOfBoundsException e) {
System.out.println ("here 1");
}
catch (Exception e) {
System.out.println ("here 2");
throw new IndexOutOfBoundsException();
}
finally {
System.out.println ("here I am");
}
System.out.println ("here 4");
}
public static void main(String[] args) throws IOException {
MExceptionMulti example = new MExceptionMulti();
try {
example.method1();
example.method2();
System.out.println ("I am after the method");
}
catch (IndexOutOfBoundsException e) {
System.out.println ("here A");
throw new IllegalArgumentException();
}
// catch (Exception e) {
// System.out.println ("here B1");
// }
// catch (Exception e) {
// System.out.println ("here B2");
// }
// catch (Throwable e) {
// System.out.println ("here B3");
// }
System.out.println ("Here at the end of it all!!!");
}
public void method2 () throws IOException
{
BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
System.out.println ("How much do you weight?");
String inputit="";
inputit = in.readLine();
int weight = Integer.parseInt(inputit);
}
}
ArrayList l=新的ArrayList(10);
此构造函数的整数参数基本上是一个提示,提示实现继续进行,并在内部为十个对象分配足够的空间。它仍然返回一个“空”数组列表。在访问这些元素之前,您需要使用
l.add(…)
向其中添加元素。您似乎对ArrayList
初始化的工作方式有误解。ArrayList l=newarraylist(10)的赋值
只会创建ArrayList
并为10
元素保留内存插槽,但不会以任何方式填充这些插槽。即使使用null
,也不可用
因此:
example.method1()上
,您的ArrayList
在ArrayList l=new ArrayList(10)上用10
插槽实例化
命令,然后立即尝试获取索引1
处的元素
ArrayList
为空。它不包含任何元素,使得1
的索引落在OutOfBounds
,从而触发OutOfBounds异常
,该异常在常规异常
捕获块中处理异常的Catch
块将here 2
文本打印到控制台,一旦超出该块的范围,程序将最终转换到块
最后
块打印我在这里
,当块超出范围时,方法也会超出范围,从而返回到main
,并显示OutOfBoundsException
新的ArrayList(10)
,只是ArrayList最初保留的大小。它不会创建10个元素(这是一个优化)。另外,请不要使用原始类型。我想我不明白为什么即使元素尚未创建,它也会抛出IndexOutOfBounds异常。如果已经创建了10的数组,从技术上讲,是否没有为该保留大小指定一个值,例如null?它还没有创建。好的,这很有意义,感谢您的解释。为什么try catch中的行没有打印“here 4”?我尝试将第一个catch上的Null指针异常更改为IndexOutOfBounds,它打印了“here4”@DieselGV这就是您所更改的吗?它应该在这里打印1
和我在这里
。是的,这就是我更改的全部内容。我在上面添加了编辑过的代码。@DieselGV哦,所以它确实在这里打印了1
和我在这里
。。。我应该从一开始就告诉你此处4
和no此处A
是因为第一个catch块没有(因此不会传播)任何抛出的(异常),因此,该方法不会在最终块之后超出范围,而是继续执行此处4
打印命令。--此后,由于没有异常,catch nmain
不会被触发(因为没有异常从method1
传播到其中),因此不会在此处打印。
ArrayList<Object> l = new ArrayList(10);