尝试捕获不被强迫

时间:2014-03-22 14:01:54

标签: java eclipse exception exception-handling try-catch

我有这个例外:

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块中,尽管该方法抛出异常(ErorrException子类Exception)。 而且我知道Exception被检查异常,所以它应该强制它,对吗?

为什么?

我该怎么做才会强迫它呢?

1 个答案:

答案 0 :(得分:1)

即使没有任何细节,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 {

}