java - 尝试捕获不被强制

标签 java eclipse exception try-catch

我有这个异常(exception):

public class ErrorException extends Exception
{
private static final long serialVersionUID = 1L;

private String errorMessage = "";
private int errorCode = 0;
private String errorLevel = "";
private Window errorSource = null;

public String getErrorMessage()
{
    return errorMessage;
}

public int getErrorCode()
{
    return errorCode;
}

public String getErrorLevel()
{
    return errorLevel;
}

public Window getErrorSource()
{
    return errorSource;
}

public ErrorException(String message, int code, int level, Window source)
{
    super();

    errorMessage = message;
    errorCode = code;

    switch (level)
    {
        case 0:
        {
            errorLevel = "benignError";
        }

        case 1:
        {
            errorLevel = "criticalError";
        }

        case 2:
        {
            errorLevel = "terminalError";
        }
    }

    errorSource = source;
}
}

我有这个方法:

public static Element check(final Document document) throws ErrorException
{
    try
                        {
                            chapter.resetLatch();

                            final SecondaryLoop loop = Toolkit.getDefaultToolkit().getSystemEventQueue().createSecondaryLoop();

                            new Thread()
                            {
                                @Override
                                public void run()
                                {
                                    SwingUtilities.invokeLater(new Runnable()
                                    {                               
                                        @Override
                                        public void run()
                                        {
                                            answer.getPreviousElement().takeFocus();
                                            question.removeAnswer(answer);
                                            question.rewriteLetters();
                                            Utils.update(chapter);
                                            loop.exit();
                                        }       
                                    });
                                }
                            }.start();

                            loop.enter();

                            chapter.getLatch().await();
                        }

                        catch (InterruptedException e)
                        {
                            throw new ErrorException("blankElementDialogError", 8, 1, Main.getGui().getMasterWindow());
                        }

return new Element();
}

我在这个构造函数代码中使用它:

public ConfirmCloseDialog(final Document document, final int postOperation)
{
    final CustomJButton doSave = new CustomJButton(Main.getString("doSave"), false);
    doSave.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent arg0)
        {   
            getConfirmCloseDialog().dispose();

            new Thread()
            {
                @Override
                public void run()
                {       
/*this method is the one above -->*/Element problem = BlankElementDialog.check(document);

                        if (problem == null)
                        {
                            new SaveChooser(document, postOperation);                   
                        }

                        else
                        {
                            new BlankElementDialog(problem);
                        }
                }
            }.start();  
        }   
    });
 }

第二部分的代码并不完整,但其余代码中没有特殊的构造(只是构造了一些 GUI 对象,并且构造函数中没有 try catch)。

然而,Eclipse 并没有强制我将方法调用封装到 try catch block 中,尽管该方法会抛出异常(ErorrException 是 Exception 的子类)。 而且我知道 Exception 是检查异常,所以它应该强制它,对吗?

为什么?

我必须做什么才能强制它?

最佳答案

即使没有 Eclipse 应该通知的任何详细信息,请查看以下内容:

enter image description here

只需重新启动 Eclipse 即可解决问题。

<小时/>
public class TestClass {
    public static void main(String[] args) {
        method(2);//Notification here!
    }
    static void method(int a) throws myException {
    }
}
class myException extends Exception {

}

关于java - 尝试捕获不被强制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22578556/

相关文章:

java - 在 Eclipse RCP 中使用拖放在 View 之间传输项目?

java - Android位置管理器编译错误

java - 设置 eclipse java compiler compliance level 1.6 是否与使用 JDK 1.6 编译相同?

java - java.lang.UnsupportedOperationException 的 HTTP 501

java - Swift 中的按位移位与 Java 不同

java - 减少 Java 进程的内存使用

eclipse - 使用 Eclipse 插件自定义项目类型

java - 在 Eclipse 中从 WAR 创建重复的 Web 项目

Java 错误处理程序

exception - 戈朗 : Defensive interfaces for nil types