异常异常与Server.main(String [])中的throws子句不兼容

时间:2014-03-15 15:49:44

标签: java exception throws

我正在通过以下链接在Eclipse Indigo上运行Lip阅读代码: https://github.com/sagioto/LipReading/blob/master/lipreading-core/src/main/java/edu/lipreading/WebFeatureExtractor.java

package main.java.edu.lipreading;

import com.googlecode.javacpp.BytePointer;
import com.googlecode.javacv.cpp.opencv_core;
import main.java.edu.lipreading.vision.AbstractFeatureExtractor;
import main.java.edu.lipreading.vision.NoMoreStickersFeatureExtractor;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketHandler;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.logging.Logger;

import static com.googlecode.javacv.cpp.opencv_core.CV_8UC1;
import static com.googlecode.javacv.cpp.opencv_core.cvMat;
import static com.googlecode.javacv.cpp.opencv_highgui.cvDecodeImage;

/**
 * Created with IntelliJ IDEA.
 * User: Sagi
 * Date: 25/04/13
 * Time: 21:47
 */
public class WebFeatureExtractor extends Server {

    private final static Logger LOG = Logger.getLogger(WebFeatureExtractor.class.getSimpleName());
    private final static AbstractFeatureExtractor fe = new NoMoreStickersFeatureExtractor();

    public WebFeatureExtractor(int port) {
        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(port);
        addConnector(connector);

        WebSocketHandler wsHandler = new WebSocketHandler() {
            public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
                return new FeatureExtractorWebSocket();
            }
        };
        setHandler(wsHandler);
    }

    /**
     * Simple innerclass that is used to handle websocket connections.
     *
     * @author jos
     */
    private static class FeatureExtractorWebSocket implements WebSocket, WebSocket.OnBinaryMessage, WebSocket.OnTextMessage {

        private Connection connection;


        public FeatureExtractorWebSocket() {
            super();
        }

        /**
         * On open we set the connection locally, and enable
         * binary support
         */
        @Override
        public void onOpen(Connection connection) {
            LOG.info("got connection open");
            this.connection = connection;
            this.connection.setMaxBinaryMessageSize(1024 * 512);
        }

        /**
         * Cleanup if needed. Not used for this example
         */
        @Override
        public void onClose(int code, String message) {
            LOG.info("got connection closed");
        }

        /**
         * When we receive a binary message we assume it is an image. We then run this
         * image through our face detection algorithm and send back the response.
         */
        @Override
        public void onMessage(byte[] data, int offset, int length) {
            //LOG.info("got data message");
            ByteArrayOutputStream bOut = new ByteArrayOutputStream();
            bOut.write(data, offset, length);
            try {
                String result = convert(bOut.toByteArray());
                this.connection.sendMessage(result);
            } catch (Exception e) {
                LOG.severe("Error in facedetection, ignoring message:" + e.getMessage());
            }
        }

        @Override
        public void onMessage(String data) {
            LOG.info("got string message");
        }
    }
    public static String convert(byte[] imageData) throws Exception {
        opencv_core.IplImage originalImage = cvDecodeImage(cvMat(1, imageData.length, CV_8UC1, new BytePointer(imageData)));
        List<Integer> points = fe.getPoints(originalImage);
        if(points == null)
            return "null";
        String ans = "";
        for (Integer point : points) {
            ans += point + ",";
        }
        return ans;
    }



    /**
     * Start the server on port 999
     */
    public static void main(String[] args) throws Exception {
        WebFeatureExtractor server = new WebFeatureExtractor(9999);
        server.start();
        server.join();
    }
}

在以下行中:

public static void main(String[] args) throws Exception {

我收到以下错误:

Exception Exception is not compatible with throws clause in Server.main(String[])

请帮我解决这个问题。

4 个答案:

答案 0 :(得分:1)

您需要检查两种情况。 1)在接口中声明方法时,需要为该方法添加throws异常,并且类似于实现该方法的接口实现类。 例如 service.java

@Component
public interface UserService {

    User getUser(Login login) throws Exception;
    }

serviceimpl.java
public User getUser(Login login)throws Exception 
    {   

       }

2)通过做上述陈述,错误仍然没有消失。确保保存这两个文件。

答案 1 :(得分:0)

Doest服务器API处理自身的所有异常。为什么不尝试删除代码中的抛出。我知道它不是很好的编程习惯,但可能会解决问题。

答案 2 :(得分:0)

问题是您正在扩展的<{1}}类已包含 具有相同Server的{​​{1}}方法} 宣言。我没看过它,但我敢打赌,这种方法根本不会抛出任何东西。

解决方案是在public static void main(String[])方法中删除您的throws子句,而不是依赖try-catches。

编辑:为什么你不能在你的案例中添加不同的throws条款。

让我们假设以下情况:

main

Java标准表示您隐藏 throws方法(或者至少尝试过)。事实是,只有class A { public static void foo() throws SomeException { ... } } class B extends A { public static void foo() throws DifferentException { ... } } }中A.foo()中的throws子句已经包含,才允许你 。因此,对于上述情况,只有当 B.foo()A.foo()的子类时,您才能完全合法。否则编译器会大叫。

答案 3 :(得分:0)

我有同样的问题,在我的情况下,我已经从未声明抛出异常的接口实现了一个方法。

在你的情况下,我猜想Server类也有一个不会抛出异常的main方法。要快速解决它。我会声明Server.main抛出异常。

此链接帮助了我

What are reasons for Exceptions not to be compatible with throws clauses?