0
votes

This is my Controller class

    @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();
}                             

I am getting the thrown exception here in catch block.

Next is my DAO class where exception is caught and thrown.

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

I have created a custom exception class and used it in my DAO class.

Here is my custom Exception class

    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;


}

}

I am interested to know whether this approach is correct or not. And how can i use the method getExceptionObj created in custom exception class in controller class, if i can.

Tell me the way to implement custom exception class method to spring in java.

1

1 Answers

0
votes

This is not right.. never catch an excpetion, if you have no idea how to handle it, check out this link here, you should use Exception handling in spring way https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc