无法使用 Java 捕获自定义异常
Not able to catch Custom Exception using Java
我正在尝试捕获 SQL 异常。我有三层,
- Controller 2. Impl class 3. DAO(流程中也有一个接口,在高层次上我放了3个层次来描述)。
控制器
try {
// Call interface which in turn will call Impl
} catch (MyException e) {
logger.debug(e);
} catch(Exception e) {
logger.debug(e);
}
return null;
实施
try {
purchaseDto = purDAO.createPurchase(clientId);
} catch(MyException e) { --> It should catch here, as I'm throwing MyException in DAO
throw e;
}catch(Exception e) { --> DAO Exception is being catch here
throw e;
}
DAO
try {
// My business logic goes here
} catch (SQLException e) {
throw new MyException (e.getErrorCode(), e.getMessage()); --> It is catching here, from here it should go back to Impl catch block of MyException
} catch (Exception e) {
e.printStackTrace();
} finally {
pt.close();
try{
if (con != null)
con.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
我的例外
public class MyException extends Exception {
/**
*
*/
private int errorCode;
private String errorDesc;
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getErrorDesc() {
return errorDesc;
}
public void setErrorDesc(String errorDesc) {
this.errorDesc = errorDesc;
}
private static final long serialVersionUID = 1L;
public MyException () {
super();
}
public MyException (int errorCode, String errorDesc) {
super();
this.errorCode = errorCode;
this.errorDesc = errorDesc;
}
}
我在 DAO 层中得到一个 sql 异常,如果我得到任何 sql 异常,我将抛出我的自定义异常。当它返回到 Impl 时,它会转到正常的异常捕获块(它作为空指针异常消息获取)。理想情况下,它应该转到我的自定义异常捕获,对吗?我哪里做错了。请指正。
如有任何想法,我们将不胜感激。
最有可能的情况是 DAO class 中的行 pt.close();
抛出异常。在那种情况下,因为异常发生在 finally 块中,所以不会抛出您的异常。
这里更详细地解释了这种情况:
我正在尝试捕获 SQL 异常。我有三层,
- Controller 2. Impl class 3. DAO(流程中也有一个接口,在高层次上我放了3个层次来描述)。
控制器
try {
// Call interface which in turn will call Impl
} catch (MyException e) {
logger.debug(e);
} catch(Exception e) {
logger.debug(e);
}
return null;
实施
try {
purchaseDto = purDAO.createPurchase(clientId);
} catch(MyException e) { --> It should catch here, as I'm throwing MyException in DAO
throw e;
}catch(Exception e) { --> DAO Exception is being catch here
throw e;
}
DAO
try {
// My business logic goes here
} catch (SQLException e) {
throw new MyException (e.getErrorCode(), e.getMessage()); --> It is catching here, from here it should go back to Impl catch block of MyException
} catch (Exception e) {
e.printStackTrace();
} finally {
pt.close();
try{
if (con != null)
con.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
我的例外
public class MyException extends Exception {
/**
*
*/
private int errorCode;
private String errorDesc;
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getErrorDesc() {
return errorDesc;
}
public void setErrorDesc(String errorDesc) {
this.errorDesc = errorDesc;
}
private static final long serialVersionUID = 1L;
public MyException () {
super();
}
public MyException (int errorCode, String errorDesc) {
super();
this.errorCode = errorCode;
this.errorDesc = errorDesc;
}
}
我在 DAO 层中得到一个 sql 异常,如果我得到任何 sql 异常,我将抛出我的自定义异常。当它返回到 Impl 时,它会转到正常的异常捕获块(它作为空指针异常消息获取)。理想情况下,它应该转到我的自定义异常捕获,对吗?我哪里做错了。请指正。
如有任何想法,我们将不胜感激。
最有可能的情况是 DAO class 中的行 pt.close();
抛出异常。在那种情况下,因为异常发生在 finally 块中,所以不会抛出您的异常。
这里更详细地解释了这种情况: