我當前的代碼塊回傳如下例外:
Exception occurred in API invocation A1-123 Fatal error
Caused by: A9-001 ColName is not found in TableName
但我想...
- 擺脫
A1 Exception A9 Exception直接顯示- 不顯示
A9 Exception在Caused by
我如何使例外看起來像這樣?
Exception occurred in API invocation A9-001 ColName is not found in TableName
<no Caused by clause>
這是我的示例代碼:
public Sample Method (Input input) throws AException
{
con = getSQLConnection();
try{
//do something
if(x==null){
throw new AException(A9ErrorMessages.A9_ERROR_FROM_TABLE, new String [] { "ColName", "TableName"});
}
}
catch (Exception e){
logger().error(e);
throw new AException(e);
}
finally{
if(con!=null){
try{
con.close();
}
catch(Exception e){
logger().error(e);
throw new AException(e);
}
}
}
}
如果它是這樣的,它會起作用嗎:
try{
//do something
}
catch (Exception e){
logger().error(e);
throw new AException(A9ErrorMessages.A9_ERROR_FROM_TABLE, new String [] { "ColName", "TableName"});
}
catch (Exception e){
logger().error(e);
throw new AException(e);
}
uj5u.com熱心網友回復:
我假設您要捕獲并傳遞的錯誤是AException- 否則您所要求的將違反該方法的約定。你可以用這樣的額外catch條款來實作你想要的。
try {
// whatever
}
catch (AException ae) {
throw ae;
}
catch (Exception e){
logger().error(e);
throw new AException(e);
}
finally {
// whatever
}
這樣,只有不是型別的例外AException才會被包裝在新AException物件中。
uj5u.com熱心網友回復:
第一個評論者是正確的。我能夠通過創建一個新的第一個捕獲來做到這一點:
public Sample Method (Input input) throws AException
{
con = getSQLConnection();
try{
//do something
if(x==null){
AException ae = new AException(A9ErrorMessages.A9_ERROR_FROM_TABLE, new String [] { "ColName", "TableName"});
logger().error(ae);
throw ae;
}
}
catch (AException ae){
logger().error(ae);
throw ae;
}
catch (Exception e){
logger().error(e);
throw new AException(e);
}
finally{
if(con!=null){
try{
con.close();
}
catch(Exception e){
logger().error(e);
throw new AException(e);
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/339639.html
