2014-07-09 61 views
-3

我正在查看以下代碼段中重新編譯了哪種類型的異常。 雖然我不確定如何去做,或者甚至可能。檢查APEX異常類型

try { 
    //SOME LOGIC 
} catch (exception ex) { 
    System.debug(//EXCEPTION TYPE); 
} 

有沒有人有任何建議或建議?

回答

1

一些摸索我找到了答案後,

try { 
    //SOME LOGIC 
} catch (Exception ex) { 
    System.debug(ex.getTypeName()); 
} 

方法很好地訣竅。

我很抱歉沒有指定我在Apex工作在我的問題。

2
try { 
//SOME LOGIC 
} catch (Exception ex) { 
    System.err.println(ex.getClass().getName()); 
} 

幾件事情:

  1. 您已經發布了異常。我假設你的意思是異常。
  2. System.debug不存在。
  3. 我已經回答了您的具體問題,但顯然這不是標準的異常處理代碼。您將輸出堆棧跟蹤,記錄異常或重新拋出一個異常。
+0

感謝您的回答JamesB,但我得到以下錯誤, 方法不存在或不正確的簽名:[例外] .getClass() – Daft

+0

其他的東西一定是你的代碼有問題。 getClass方法屬於java.lang.Object,所以每個對象都有這個方法。 – JamesB

1

首先,在您的catch塊中指定例外。在語義上,type包括Exception不屬於駝峯的情況。

其次,可以通過Exception.getClass().getName()獲得異常類型:

catch (Exception exception) { 
    System.out.println(exception.getClass().getName()); 
    // exception.printStackTrace(); 
    // throw exception; 
} 

指定者:
http://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html