我有一个自定义例外延伸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