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 {
} catch (exception ex) {
    System.debug(//EXCEPTION TYPE);

Would anyone have any suggestions or advice??


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

Example 1:

try {
} catch (Exception ex) {

Example 2:

Exception e = new MyCustomException();
// 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.