English 中文(简体)
is it good to catch error in DAO?
原标题:
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.

问题回答

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;
}




相关问题
handling exceptions IN Action Filters

Is there a better way to handle exceptions that occur inside an Action Filter itself in ASP .NET MVC? There re 2 ways I can think of at the moment. Using a try catch and setting the HTTP Status ...

既可捕获,又可举出例外。

我有一种办法,可以进入亚洲开发银行,因此,我国的亚行在多瑙河航道中的所有 st子都位于一个试捕区。 它正在追捕Kexception

Cross compiler exception handling - Can it be done safely?

I am doing some maintenance on a C++ windows dll library that is required to work with different VC++ compilers (as I don’t want to address different mangling schemes). I have already eliminated any ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

Watch a memory location/install data breakpoint from code?

We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. ...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签