在 DAO 中捕获错误有好处吗?

发布于 2024-08-12 02:42:21 字数 1856 浏览 4 评论 0原文

public boolean checkInd() {
    int dis_ind = 2;
    HashMap parmMap = new HashMap();
    //line below can generate errors
    getSqlMapClientTemplate().queryForList("authentication.checkInd", parmMap);
    List results = (List) parmMap.get("Result0");
    HashMap resultMap;
    if (result.size()>0)
        resultMap = (HashMap)resultMap.get(0); 
    dis_ind = (Integer)resultMap.get("PK_VALUE");
    if (dis_ind == 1)
        return true;
    else
        return false;
}

代码的第 5 行可能会产生错误。仅举几例,错误可能是...映射中提到的存储过程不存在,映射中提到的列名不存在..等等。

如果我没有捕获错误并且出现问题,则日志跟踪将写入server.log 并且 Web 应用程序停止在我的 web.xml 中提到的错误页面

如果我捕获错误,那么 Web 应用程序不会停止在错误页面并继续其快乐方式(谁知道现在会停在哪里)。

所以现在我想不要在这里发现错误。

问题是我不想在 server.log 中写入堆栈跟踪(如上所述,现在默认情况下会发生这种情况)。相反,我想将堆栈跟踪写入名为 myapp.log 的文件(我们使用的是 log4j)。但是,我不知道如何在不实际捕获错误的情况下将堆栈跟踪显式写入 myapp.log 。下面是我要编写的代码(但应用程序不会停止,因为我捕获了错误并将继续运行,这不好)。

OurLogger log = OurLogger.getLogger("myapp", MyClassName.class);
public boolean checkInd() {
    int dis_ind = 2;
    try {
    HashMap parmMap = new HashMap();
    //line below can generate errors
    getSqlMapClientTemplate().queryForList("authentication.checkInd", parmMap);
    List results = (List) parmMap.get("Result0");
    HashMap resultMap;
    if (result.size()>0)
        resultMap = (HashMap)resultMap.get(0); 
        dis_ind = (Integer)resultMap.get("PK_VALUE");
    }
    catch (BadSqlGrammarException e) {log.error(e, e.getMessage());}
        if (dis_ind == 1)
            return true;
        else
            return false;
}

在这种情况下我有哪些选择以及最佳实践是什么?

PS:这个问题与我的其他问题相关 但我尝试在这个问题中添加示例并寻求最佳实践建议。

public boolean checkInd() {
    int dis_ind = 2;
    HashMap parmMap = new HashMap();
    //line below can generate errors
    getSqlMapClientTemplate().queryForList("authentication.checkInd", parmMap);
    List results = (List) parmMap.get("Result0");
    HashMap resultMap;
    if (result.size()>0)
        resultMap = (HashMap)resultMap.get(0); 
    dis_ind = (Integer)resultMap.get("PK_VALUE");
    if (dis_ind == 1)
        return true;
    else
        return false;
}

5th line of the code can generate errors. To name a few, errors might be...Stored Procedure mentioned in mapping does not exist, column name mentioned in mapping does not exist..etc..

If I do not catch the error and something goes wrong then log trace is written on server.log and the webapplication stops at the error page mentioned in my web.xml

If I catch the error then web app does not stop at error page and continues on its merry way (who knows where it will stop now).

So by now I am thinking NOT to catch errors here.

Problem is that I do not want to write the stack trace in server.log (which happens by default right now, as mentioned above). Instead I want to write stack trace to a file called myapp.log (we are using log4j). However, i do not know a way to explicitly write stack trace to myapp.log without actually catching the error. Below is the code I would write (but again the app will not stop since i am catching the error and will keep going, which is not good).

OurLogger log = OurLogger.getLogger("myapp", MyClassName.class);
public boolean checkInd() {
    int dis_ind = 2;
    try {
    HashMap parmMap = new HashMap();
    //line below can generate errors
    getSqlMapClientTemplate().queryForList("authentication.checkInd", parmMap);
    List results = (List) parmMap.get("Result0");
    HashMap resultMap;
    if (result.size()>0)
        resultMap = (HashMap)resultMap.get(0); 
        dis_ind = (Integer)resultMap.get("PK_VALUE");
    }
    catch (BadSqlGrammarException e) {log.error(e, e.getMessage());}
        if (dis_ind == 1)
            return true;
        else
            return false;
}

What are my options in this situation and what are the best practices?

PS: This question is related to my other question but i've tried to add examples in this question and seek best practice advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋你朝朝暮暮 2024-08-19 02:42:21

捕捉并不需要是“永久的”。只需捕获它,记录它,然后重新抛出错误。就像这样:

catch (BadSqlGrammarException e) {
  log.error(e, e.getMessage());
  throw e;
}

Catching doesn't need to be "permanent". Simply catch it, log it, and then rethrow the error. Like so:

catch (BadSqlGrammarException e) {
  log.error(e, e.getMessage());
  throw e;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文