使用public static main(String args)执行performExecute()

时间:2013-07-11 13:28:24

标签: java main public-method

 public class XGetProgramGuideDirectTestControllerCmdImpl extends ControllerCommandImpl implements XGetProgramGuideDirectTestControllerCmd {
    public final String CLASSNAME = this.getClass().getName();
    public void performExecute() throws ECException { /* code is here*/ }

    private void callUSInterface() throws ECException { /* code is here*/ }

    private void callDEInterface() throws ECException { /* code is here*/ }

    private void callUKInterface() throws ECException { /* code is here*/ }

    public void setRequestProperties(TypedProperty req) throws ECException { /* code is here*/ }

    private void displayResponse(StringBuffer testResult) { /* code is here*/ }

    public static void main(String[] args) {
        XGetProgramGuideDirectTestControllerCmdImpl PGDirTestController = new XGetProgramGuideDirectTestControllerCmdImpl();
        PGDirTestController.performExecute();
    }

}

我只是尝试使用public static void main(String[] args)在Eclipse-RAD中将此应用程序作为Java应用程序运行,但它给出了一个错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Unhandled exception type ECException 

在:

PGDirTestController.performExecute();

请放轻松我,我对Java仍然很陌生。

2 个答案:

答案 0 :(得分:3)

因为你宣布:

public void performExecute() throws ECException 

然后你被迫处理ECException

因此,当您调用它时,您应该使用try-catch将其包围,或者将您调用它的方法声明为throw异常:

public static void main(String[] args) {
      XGetProgramGuideDirectTestControllerCmdImpl PGDirTestController = new 
               XGetProgramGuideDirectTestControllerCmdImpl();
      try {
          PGDirTestController.performExecute();
      } catch(ECException e) { 
            e.printStackTrace();
            //Handle the exception!
        }
}

OR

public static void main(String[] args) throws ECException {
     XGetProgramGuideDirectTestControllerCmdImpl PGDirTestController = new 
              XGetProgramGuideDirectTestControllerCmdImpl();
     PGDirTestController.performExecute();
}

答案 1 :(得分:1)

首先,变量应该按照Java convention的小写字母开头,否则会让人感到困惑:

XGetProgramGuideDirectTestControllerCmdImpl pGDirTestController = new XGetProgramGuideDirectTestControllerCmdImpl();

关于您的问题,未处理的异常类型表示此方法抛出的异常不是RuntimeException而您没有处理它。在Java中,您必须显式捕获所有不是RuntimeException的子代的异常。

try {
    pGDirTestController.performExecute();
} catch (final ECException e) {
    // Do whatever you need to do if this exception is thrown
}

只要抛出ECException,就会执行catch部分。您应该在此处添加代码以处理抛出此异常时要执行的操作。我强烈建议你不要把这个问题留空,因为如果抛出异常,你永远不会知道。

如果您将使用Java,我强烈建议您获得Java书籍/教程。这是非常基本的东西,所以你更好地理解这一点。祝你好运。