考慮我有一個包含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.
你認爲這樣的日誌消息會有什麼幫助嗎? –