【问题标题】:exception handling framework in spring javaSpring java中的异常处理框架
【发布时间】:2016-05-23 10:34:16
【问题描述】:

这是我的控制器类

    @RequestMapping(value = "/profile/MyLink.do", method = RequestMethod.POST)
public void MyLink(HttpServletRequest request,
        HttpServletResponse response, @RequestBody String data)
                throws JSONException {


        JSONObject profile = null;
        try {
            profile = new JSONObject(data.toString());
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            System.out.println(e1.getMessage());
        }
        // Some code

        // calling the DAO method from where the exception will be thrown.
        } catch (Exception e) {

            // checking the thrown message from method
            if(e.getMessage() != null && e.getMessage().length() > 0){
                logger.info("Printing the error message " + e.getMessage());

                JsonReader reader = new JsonReader(new StringReader(e.getMessage()));
                reader.setLenient(true);

                //creating Java Object from the thrown message
                objException = gson.fromJson(reader, ExceptionBean.class);
            }

            returnProfile.setError("error");

            if(objException != null)
                returnProfile.setObjException(objException);
            //e.printStackTrace();
        }


    strReturnJsonString = gson.toJson(returnProfile);

    PrintWriter pw = null;
    try {
        pw = response.getWriter();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    pw.write(strReturnJsonString.trim());
    pw.flush();
    pw.close();
}                             

我在 catch 块中得到了抛出的异常。

接下来是我的 DAO 类,在其中捕获并抛出异常。

    public boolean MyDaoMethod()
{


    boolean blnResult = false;
    String strInsertQuery =" some sql query"; 
    try {
        for (Vertex v : (Iterable<Vertex>) graph.command(
                new OCommandSQL(strInsertQuery)).execute()) {

    }
    catch(OCommandExecutorNotFoundException e2){
        logger.info("Printing whole exception !!! " + gson.toJson(e2.getStackTrace()));
        logger.info("Printing the further details for exception !!!! " + Thread.currentThread().getStackTrace()[1].getClassName() + " " + Thread.currentThread().getStackTrace()[1].getMethodName() + " " + Thread.currentThread().getStackTrace()[1].getLineNumber());
        ExceptionBean objException = new ExceptionBean();
        objException.setStrExceptionCode("202");
        objException.setStrExceptionDesc("Cannot find a command executor for the command request : "+strInsertQuery);
        objException.setStrFileName(Thread.currentThread().getStackTrace()[1].getFileName());
        objException.setStrClassName(Thread.currentThread().getStackTrace()[1].getClassName());
        objException.setStrMethodName(Thread.currentThread().getStackTrace()[1].getMethodName());

        for(int i=0;i<e2.getStackTrace().length;i++){
            if(objException.getStrClassName().equalsIgnoreCase(e2.getStackTrace()[i].getClassName()) 
                    && objException.getStrMethodName().equalsIgnoreCase(e2.getStackTrace()[i].getMethodName())){
                objException.setStrLineNumber(""+e2.getStackTrace()[i].getLineNumber());
            }
        }

        String strExceptionObj = gson.toJson(objException);
        throw new OrientDBCommandException(strExceptionObj);
    }catch(OCommandSQLParsingException e1){
        logger.info("Printing whole exception !!! " + gson.toJson(e1.getStackTrace()));
        logger.info("Printing the further details for exception !!!! " + Thread.currentThread().getStackTrace()[1].getClassName() + " " + Thread.currentThread().getStackTrace()[1].getMethodName() + " " + Thread.currentThread().getStackTrace()[1].getLineNumber());
        ExceptionBean objException = new ExceptionBean();
        objException.setStrExceptionCode("201");
        objException.setStrExceptionDesc("There is some kind of error in DataBase command parsing.");
        objException.setStrFileName(Thread.currentThread().getStackTrace()[1].getFileName());
        objException.setStrClassName(Thread.currentThread().getStackTrace()[1].getClassName());
        objException.setStrMethodName(Thread.currentThread().getStackTrace()[1].getMethodName());

        for(int i=0;i<e1.getStackTrace().length;i++){
            if(objException.getStrClassName().equalsIgnoreCase(e1.getStackTrace()[i].getClassName()) 
                    && objException.getStrMethodName().equalsIgnoreCase(e1.getStackTrace()[i].getMethodName())){
                objException.setStrLineNumber(""+e1.getStackTrace()[i].getLineNumber());
            }
        }

        String strExceptionObj = gson.toJson(objException);
        throw new OrientDBCommandException(strExceptionObj);

    }
         catch (Exception e) {
             logger.error(e);
     }      

    return blnResult;
}

我创建了一个自定义异常类并在我的 DAO 类中使用它。

这是我的自定义异常类

    public class OrientDBCommandException extends OCommandSQLParsingException{

String ObjException; 

public OrientDBCommandException(String ObjException) {
    super(ObjException);

    this.ObjException=ObjException;
}

public ExceptionBean getExceptionObj(String strExceptionCode){

    ExceptionBean objException = new ExceptionBean();

    return objException;


}

}

我很想知道这种方法是否正确。如果可以的话,我如何使用在控制器类的自定义异常类中创建的方法 getExceptionObj。

告诉我如何在java中实现自定义异常类方法到spring。

【问题讨论】:

    标签: java spring spring-mvc exception-handling orientdb


    【解决方案1】:

    这是不对的..永远不要遇到异常,如果您不知道如何处理,请在此处查看此链接,您应该使用Exception handling in spring way https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

    【讨论】:

    • 我对响应状态和定义错误视图不太满意。我的主要目标是捕获异常并为其创建一个对象,该对象将定义与异常相关的所有细节。
    • @DineshPurty 详细信息应被异常处理程序抛出和捕获。以您的方式,您只会丢失有关异常实际原因的信息
    • 如您所见...我正在收集 DAO 类方法中存在的 catch 块中的所有信息。并且,从那里抛出到我的自定义异常类,作为回报,它被收集在控制器类的 catch 块中。在控制器类中,我获得了有关捕获的异常的所有必需信息。
    • @DineshPurty 我明白你的意思,这是可行的.. 但这不是最佳实践.. 代码在你手中.. 只要它有效,你可以做任何你想做的事情;)
    • 好的..感谢您的建议。真的很感激。如果你必须这样做,以你的方式,你会如何解决。你能提供一个示例演示吗?
    猜你喜欢
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 2012-06-10
    • 2014-07-11
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    相关资源
    最近更新 更多