如何处理struts2中的深度嵌套异常



My struts2 Web应用程序使用SQL数据库。在DB访问代码中,我编写了一个基本的try/catch处理程序,用于捕获SQL或一般异常,将详细信息写入日志文件,然后继续。类的层次结构如下:

Action方法->在Model上获取或设置方法->DB访问。

//Action method in action class
public string doActionMethod() throws Exception
{
    String results = SampleModel.getResults();
}
//Model method in model class
public string getResults() throws Exception
{
    String results = DBLayer.runQuery("SELECT Results FROM SampleTable WHERE Value='1');
}
//Method that queries database in DB access class
public string runQuery() throws Exception
{
    ResultSet rs = null;
    Connection dbConnection = null;
    PreparedStatement preparedStatement = null;     
    dbConnection = MSSQLConnection.getConnection();
    preparedStatement = dbConnection.prepareStatement(sqlQuery);
    //run SQL statements
    return String(rs.get(0));
}

我希望捕捉到的异常能够冒泡到Action级别,这样我就可以将它们转发到适当的错误页面。有比在方法签名中添加"throws Exception"更好的方法吗?

由于您没有恢复的希望,请抛出一个特定于应用程序的RuntimeException

使用标准Struts2声明性异常处理将您的应用程序转到适当的错误页面。

最新更新