在GWT中异步方法中传递异常的正确方法是什么?

时间:2014-10-03 15:05:22

标签: java gwt asynchronous

我不确定在GWT中异步方法中传递异常的正确方法是什么。我有一个dataservice连接到数据库,查询信息,然后将其返回给客户端。但是,如果发生检查异常以及如何处理它,我根本不会理解该怎么做。

目前我有类似的东西

public int addAssembler(int assemblerID, String name, boolean active) {
        Connection con = connect();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            String query = "INSERT INTO assemblers (name, hidden) VALUES ('" + name + "'," + active + ")";
            ps = con.prepareStatement(query);
            System.out.println("addAssembler " + ps.toString());
            ps.executeUpdate(query);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(rs, ps, con);
        }
        return 1;
    }

在这里被称为

public void onClick(ClickEvent event) {
                Ioma.dataservice.addAssembler(0, name.getText(), !isActive.getValue(), new AsyncCallback<Integer>() {
                    @Override
                    public void onFailure(Throwable caught) {
                        Window.alert("Failed to add assembler! Please check error log" + caught.toString());
                    }

                @Override
                public void onSuccess(Integer result) {
                    Window.alert("Assembler " + name.getText() + " added");
                }
            });

我意识到SQLException是在我的try / catch中捕获的,但是我想要失败的方法,并且我想将它传回失败,我该怎么做?我试过了

public int addAssembler(parameters) throws SQLException {}

但它为我提供了SQLException的源代码未找到错误。我甚至不确定这是否是正确的方法来做到这一点?有没有更好的办法?

1 个答案:

答案 0 :(得分:2)

请阅读文档的this section

你不能抛出SQLException,因为客户端不知道那是什么。您可以做的是定义您自己的异常类型(在您的客户端或共享包中):

public class DatabaseException extends Exception implements IsSerializable
{
    private static final long serialVersionUID = -7935074533073743071L;

    public DatabaseException()
    {
        super();
    }

    public DatabaseException(String message)
    {
        super(message);
    }

    public DatabaseException(Exception e)
    {
        super(e);
    }
}

然后在您的服务方法中声明它:

public interface MyService extends RemoteService {
    int addAssembler(int assemblerID, String name, boolean active) throws DatabaseException;
}

然后你可以把它扔到服务器端:

public int addAssembler(int assemblerID, String name, boolean active) throws DatabaseException {
    Connection con = connect();
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        String query = "INSERT INTO assemblers (name, hidden) VALUES ('" + name + "'," + active + ")";
        ps = con.prepareStatement(query);
        System.out.println("addAssembler " + ps.toString());
        ps.executeUpdate(query);
    } catch (SQLException e) {
        throw new DatabaseException(e); // <----- THIS IS IMPORTANT
    } finally {
        close(rs, ps, con);
    }
    return 1;
}

在客户端上,将调用onFailure(Throwable caught)方法,您可以执行以下操作:

@Override
public void onFailure(Throwable caught)
{
    if(caught instanceof DatabaseException)
        Window.alert(caught.getLocalizedMessage());
}