ArrayIndexOutOfBoundsException 的自定义异常

发布于 2024-11-08 18:37:38 字数 1376 浏览 0 评论 0原文

我有一个扩展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");
                }

            }

ProblemscommandValue 是枚举。
这是我的异常类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜夜流光相皎洁 2024-11-15 18:37:38

当您扩展 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.

风苍溪 2024-11-15 18:37:38

添加

catch (ArrayIndexOutOfBoundsException e) {

}

到您的 catch 语句中。

ArrayIndexOutOfBoundExceptionCommandSyntaxException 是不同的异常,如果您想捕获它们,您应该单独捕获每个异常,或者捕获它们共同祖先的异常(Exception)。

更新
如果您现在想在 1 个 catch 子句中捕获,您可以

  1. 等待 java 7 http://www.baptiste-wicht.com/2010/05/better-exception-handling-in-java-7-multicatch-and-final-rethrow/
  2. 让您的 CommandSyntax 成为 ArrayIndexOutOfBounds 的后继者

Add

catch (ArrayIndexOutOfBoundsException e) {

}

to you catch statements.

ArrayIndexOutOfBoundException and CommandSyntaxException 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

  1. wait for java 7 http://www.baptiste-wicht.com/2010/05/better-exception-handling-in-java-7-multicatch-and-final-rethrow/
  2. Make your CommandSyntax successor of ArrayIndexOutOfBounds
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文