Search code examples
exceptionapex-code

Check APEX exception type


I'm looking to check what type of exception is retruned in the following code snippet. I'm not sure exactly how to do it though, or if it's even possible.

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

Would anyone have any suggestions or advice??


Solution

  • After some searching around I found an answer,

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

    The getTypeName() method does the trick nicely.

    I apologise for not specifying I was working in Apex in my question.