我正在开发 Java Web 服务 (SOAP),并使用异常处理。一些操作可能会抛出 IOExceptions。尽管我使用 try-catch 语句处理它们,但它们仍然被发送回客户端。如何在不将异常发送回客户端的情况下继续执行服务?
例如我有以下一段代码
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;
}
此函数由以下函数调用,该函数是 Web 服务 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;
}
可能抛出异常的部分如下:
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;
}
如何确保执行不会停止并且客户端看不到异常?
谢谢你,尼克