6

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??

7

There is a getTypeName() method on the Exception class that you can use. It will output the actual instance's type.

Example 1:

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

Example 2:

Exception e = new MyCustomException();
System.debug(e.getTypeName()); 
// outputs 'MyCustomException', not 'Exception'.
| improve this answer | |
  • 4
    Can also make use of 'e instanceof DmlException' etc – bigassforce Jul 9 '14 at 14:32
  • 4
    You can also catch specific exceptions, as in catch(DmlException e) { ... } catch(NoAccessException e) { ... } ... – sfdcfox Jul 9 '14 at 14:40
  • Excellent, cheers guys! – Daft Jul 10 '14 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.