简体   繁体   English

如何在不将异常转发给客户端的情况下在JAX-WS Web服务上捕获异常

[英]How to catch Exceptions on a JAX-WS web service without forwarding them to the client

I am working on a Java Web Service (SOAP) and I make use of Exception handling. 我正在使用Java Web服务(SOAP),并且使用了异常处理。 Some of the operations may throw IOExceptions. 某些操作可能会引发IOException。 Despite the fact that I handle them with try-catch statements, they are still sent back to the client. 尽管我使用try-catch语句来处理它们,但它们仍然被发送回客户端。 How can I continue the execution of the Service without sending the Exception back to the client? 如何在不将异常发送回客户端的情况下继续执行服务?

For example I have the following piece of code 例如我有以下代码

synchronized public int parseDataModelFromDatabase(Connection con, Logger webAppLog) {
    FileWriter fStream;
    BufferedWriter out;
    Connection dbCon;
    Statement st;
    this.data_model = null;
    File f = new File(this.OUTPUT_DIR + "/" + this.data_model_file);
    if(f.exists()) {
        f.delete();
    }
    try {
        f.createNewFile();
    }catch(IOException e) {
        System.err.println("ItemSimilarityModel.parseDataModelFromDatabase(): Failed to create output file: "
                + e.getMessage());
        webAppLog.error("ItemSimilarityModel.parseDataModelFromDatabase() threw an IOException when attempted to create new data file: ", e);
        return -1;
    }
    if(!f.exists()) {
        return -2;
    }
    try {
        fStream = new FileWriter(this.OUTPUT_DIR + "/" + this.data_model_file);
        out = new BufferedWriter(fStream);
    }catch(Exception e) {
        System.err.println("ItemSimilarityModel.parseDataModelFromDatabase(): Failed to open output file: "
                + e.getMessage());
        webAppLog.error("ItemSimilarityModel.parseDataModelFromDatabase(): Failed to open output data file: ", e);
        return -3;
    }

    String selectStr = "SELECT * FROM " + this.dbTableUserPrefs;
    try {
        dbCon = (Connection) con;
        if(dbCon != null && dbCon.isValid(0) == true) {
            st = (Statement) dbCon.createStatement();
            ResultSet rs = st.executeQuery(selectStr);
            while(rs.next()) {
                out.write(rs.getString(1) + "," + rs.getString(2) + "," +
                        rs.getString(3) + "," + rs.getString(4) + "\n");
            }
            out.close();
            dbCon.close();
        }
    }catch(SQLException e) {
        System.err.println("ItemSimilarityModel.parseDataModelFromDatabase(): SQL Exception occured: "
                + e.getMessage());
        webAppLog.error("ItemSimilarityModel.parseDataModelFromDatabase(): SQLException thrown on writing to the data model file.", e);
        return -7;
    }catch(IOException e) {
        System.err.println("ItemSimilarityModel.parseDataModelFromDatabase(): IO Exception occured: "
                + e.getMessage());
        webAppLog.error("ItemSimilarityModel.parseDataModelFromDatabase(): IOException thrown on writing to the data model file.", e);
        return -8;
    }
    try {
        if(f != null && f.isFile() == true && f.exists() == true)
            this.data_model = new FileDataModel(f);
    }catch(FileNotFoundException e) {
        System.err.println("ItemSimilarityModel.parseDataModelFromDatabase(): FileNotFoundException message: "
                + e.getMessage());
        webAppLog.error("ItemSimilarityModel.parseDataModelFromDatabase(): FileNotFoundException thrown on parsing the data model file.", e);
        return -5;
    }catch(IOException e) {
        System.err.println("ItemSimilarityModel.parseDataModelFromDatabase(): IOException message: "
                + e.getMessage());
        webAppLog.error("ItemSimilarityModel.parseDataModelFromDatabase(): IOException thrown on parsing the data model file.", e);
        return -6;
    }
    return 0;
}

This function is called by the following function which is part of the Web Service API: 此功能由Web Service API的以下功能调用:

public int ReloadRecommendationModel(@WebParam(name = "username") String uname, @WebParam(name = "password") String pass) {
    System.out.println("EngineModule.ReloadRecommendationModel called.");
    if(uname.equals(this.cap_recommender_uname) == false || pass.equals(this.cap_recommender_pass) == false) {
        return -1;
    }
    DataSource ds_preferences = null;
    DataSource ds_similarities = null;
    Connection con_preferences;
    Connection con_similarities;
    try {
        Context initContext = new InitialContext();
        Context envContext = (Context) initContext.lookup("java:/comp/env");
        ds_preferences = (DataSource) envContext.lookup("jdbc/user_history");
        ds_similarities = (DataSource) envContext.lookup("jdbc/similarity_model");
    }catch(NamingException e) {
        System.err.println(e.getMessage());
        webAppLog.error("EngineModule.ReloadRecommendationModel(): Error occured while obtaining context.", e);
    }
    int val = 0;
    try {
        if(ds_preferences != null && ds_similarities != null) {
            con_preferences = (Connection) ds_preferences.getConnection();
            con_similarities = (Connection) ds_similarities.getConnection();
            synchronized(SingletonItemSimilarityModel.getModel()) {
                val = (SingletonItemSimilarityModel.getModel()).parseDataModelFromDatabase(con_preferences, webAppLog);
                if(val != 0) {
                    webAppLog.error("EngineModule.ReloadRecommendationModel(): parseDataModelFromDatabase() returned: " + val + ".");
                    return 0;
                }
                val = (SingletonItemSimilarityModel.getModel()).parseSimilarityModelFromDatabase(con_similarities, webAppLog);
                if(val != 0) {
                    webAppLog.error("EngineModule.ReloadRecommendationModel(): parseSimilarityModelFromDatabase() returned: " + val + ".");
                    return 0;
                }
            }
        }
        webAppLog.info("EngineModule.ReloadRecommendationModel(): Reload operation of data-model completed with return code: " + val + ".");
    }catch(SQLException e) {
        System.err.println("EngineModule.ReloadRecommendationModel(): Error occured while attempting to connect to Recommender data-source: " +
                 e.getMessage());
        webAppLog.error("EngineModule.ReloadRecommendationModel(): Error occured while attempting to connect to Recommender data-source.", e);
    }
    return val;
}

The part that may throw an Exception is the following: 可能引发异常的部分如下:

try {
    if(f != null && f.isFile() == true && f.exists() == true)
        this.data_model = new FileDataModel(f);
}catch(FileNotFoundException e) {
    System.err.println("ItemSimilarityModel.parseDataModelFromDatabase(): FileNotFoundException message: "
            + e.getMessage());
    webAppLog.error("ItemSimilarityModel.parseDataModelFromDatabase(): FileNotFoundException thrown on parsing the data model file.", e);
    return -5;
}catch(IOException e) {
    System.err.println("ItemSimilarityModel.parseDataModelFromDatabase(): IOException message: "
            + e.getMessage());
    webAppLog.error("ItemSimilarityModel.parseDataModelFromDatabase(): IOException thrown on parsing the data model file.", e);
    return -6;
}

How can I make sure that the execution will not stop and the Exception is not seen from the Client? 如何确保执行不会停止并且从客户端看不到异常?

Thank you, Nick 谢谢尼克

Consider wrapping your code in a try-catch(Throwable e) . 考虑将代码包装在try-catch(Throwable e) This should catch everything. 这应该抓住一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM