考虑我有一个包含 3 条语句的 try 块,它们都导致异常。我希望所有 3 个异常都由其相关的 catch 块处理。这可能吗?
像这样-->
class multicatch
{
public static void main(String[] args)
{
int[] c={1};
String s="this is a false integer";
try
{
int x=5/args.length;
c[10]=12;
int y=Integer.parseInt(s);
}
catch(ArithmeticException ae)
{
System.out.println("Cannot divide a number by zero.");
}
catch(ArrayIndexOutOfBoundsException abe)
{
System.out.println("This array index is not accessible.");
}
catch(NumberFormatException nfe)
{
System.out.println("Cannot parse a non-integer string.");
}
}
}
是否可以获得以下输出?-->>
Cannot divide a number by zero.
This array index is not accessible.
Cannot parse a non-integer string.