ArrayIndexOutOfBoundsException 的自定义异常
我有一个扩展Exception
的自定义异常,但它似乎没有捕获ArrayIndexOutOfBoundsException
。但是,如果我将 catch 子句更改为 catch Exception
,它就会按预期工作。
超类异常不应该捕获子类异常,即使它是 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");
}
}
Problems
和 commandValue
是枚举。
这是我的异常类:
public class CommandSyntaxException extends Exception {
private String message;
public CommandSyntaxException(String message) {
this.message = message;
}
@Override
public String getMessage() {
return message;
}
}
有没有任何解决方法(除了捕获异常
)? 我的意图是在单个 catch 子句中捕获所有异常以及我自己的异常。
I have a custom exception extending Exception
, however it does not seem to catch ArrayIndexOutOfBoundsException
. But if I change the catch clause to catch Exception
instead, it works as expected.
Shouldn't a superclass exception catch a subclass exception even though it is a RuntimeException
?
Here is the cause of the exception:
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");
}
}
Problems
and commandValue
are enums.
And here is my exception class:
public class CommandSyntaxException extends Exception {
private String message;
public CommandSyntaxException(String message) {
this.message = message;
}
@Override
public String getMessage() {
return message;
}
}
Is there any workaround (Besides catching Exception
) ? My intension is to catch all exceptions with my own exception in a single catch clause.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您扩展 Exception 并创建 CommandSyntaxException 时,它会成为特定的异常。现在您试图捕获 CommandSyntaxException 但该异常不会抛出,而是 ArrayIndexOutOfBound 是线程,因此不会被捕获。
如果您的代码抛出 CommandSyntaxException,那么只有它会被捕获。 :)
可以通过三种方式快速解决此问题。
CommandSyntaxException 扩展了 RuntimeException
或者 CommandSyntaxException 扩展 ArrayIndexOutOfBoundException。
或者您的代码抛出 CommandSyntaxException 。
“我的意图是在单个 catch 子句中捕获所有异常以及我自己的异常”:
您可以使用 Catch(Exception e) 捕获所有异常,但是使用单个 catch 子句捕获所有异常并不是一个好的做法。
When you are extending Exception and creating CommandSyntaxException it becomes a specific exception. And now you are trying to catch CommandSyntaxException but that exception is not thrown instead ArrayIndexOutOfBound is thread, so it will not be caught.
If your code throws CommandSyntaxException then only it will be caught. :)
quick fix to this problem can be in three ways.
CommandSyntaxException extends RuntimeException
Or CommandSyntaxException extends ArrayIndexOutOfBoundException.
Or your code throws CommandSyntaxException .
"My intension is to catch all exceptions with my own exception in a single catch clause" :
You can catach all exceptions using Catch(Exception e) But catching all exception with a single catch clause is not a good practice.
添加
到您的 catch 语句中。
ArrayIndexOutOfBoundException
和CommandSyntaxException
是不同的异常,如果您想捕获它们,您应该单独捕获每个异常,或者捕获它们共同祖先的异常(Exception)。
更新
如果您现在想在 1 个 catch 子句中捕获,您可以
Add
to you catch statements.
ArrayIndexOutOfBoundException
andCommandSyntaxException
are different exception and if you want to catch them both you should either catch every exception separately or catch exception which is their common ancestor (Exception
).Update
If you now want to catch in 1 catch clause you can