我有一個自定義例外延伸Exception
,但它似乎並沒有趕上ArrayIndexOutOfBoundsException
。但是,如果我改變捕捉條款來捕捉Exception
,它會按預期工作。ArrayIndexOutOfBoundsException的自定義異常
不應該超類異常捕獲子類異常,即使它是RuntimeException
?
這裏是例外的原因:
int timeInMillis = 0;
for (int i = 0; i < commandMessage.length; i++)
for (String commandValue : command.getArguments()) {
try {
if (commandValue.equals(commandMessage[i]))
// This is causing it.
timeInMillis =
Integer.parseInt(commandMessage[i + 1]);
else
throw new CommandSyntaxException(Problems.
SYNTAX_ERROR.getProblemDescription());
} catch (CommandSyntaxException commandSyntaxException) {
System.out.println("foo");
}
}
和commandValue
是枚舉。
這裏是我的異常類:
public class CommandSyntaxException extends Exception {
private String message;
public CommandSyntaxException(String message) {
this.message = message;
}
@Override
public String getMessage() {
return message;
}
}
有任何解決方法(除了醒目Exception
)? 我的意圖是在單個catch子句中用我自己的異常捕獲所有異常。
Downvoted?請解釋! – whirlwin 2011-05-23 10:20:07