【发布时间】: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