考虑我有一个包含3条语句的try块,并且它们都会导致异常。我希望所有的3个例外都能通过相关的catch块来处理..有可能吗?我可以执行多个与一个try块对应的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.
你认为这样的日志消息会有什么帮助吗? –