An Introduction to Exception Handling in Apex

Last Updated on Tuesday, 12 January 2010 10:48 Written by Steve Tuesday, 12 January 2010 10:48

I was privileged to get to write an article for DeveloperForce.com on exception handling in Apex. I wrote this article not from the perspective of an expert on the subject, but as someone who really wanted to learn more about my options for using exception handling in my Apex code.

I’ve always had a nagging feeling that I wasn’t following best practices in my exception handling in Apex. In most cases I wasn’t doing any exception handling at all. I decided to write up what I have learned about error handling on the Force.com platform so that others could jump into it full-bore, and make their programs more robust and fault tolerant.

I hope it’s helpful to Apex programmers out there. Let me know what you think!

Comments

Powered by Facebook Comments


6 Comments

  1. Jason   |  Tuesday, 12 January 2010 at 10:55 am

    Very nice. Was the partial processing piece able to help with troubles you were encountering we discussed in the developer meeting.

    I guess it would only help with DML errors but thats better than nothing I suppose.

  2. Steve   |  Tuesday, 12 January 2010 at 11:00 am

    I’m pretty sure partial processing will let you do the post-commit processing we talked about–write to a custom object, post to a web service, send an email. By using partial processing in DMLs, you don’t actually throw exceptions, and you don’t get automatic rollback, but you do get to continue on in the processing stack as long as your DML is well formed.

  3. Gary B   |  Wednesday, 20 January 2010 at 7:08 am

    I’ve done some heavy duty coding for my current project, and during test there have been several points in my code where I wish I’d used exceptions, or points where I intended to put exceptions in but didn’t get round to it.

    My advice – start doing it as soon as you start writing your code! It’s not difficult nor time consuming, and allows for much neater and cleaner solutions. For some reason, I thought exceptions were something to do after you’d written your code to try and mop up awkward situations or errors, but in fact they’re a useful tool to use throughout development.

    That’s not just applicable to Salesforce either :)

  4. Steve   |  Wednesday, 20 January 2010 at 7:09 am

    Thanks Gary! Good advice.

  5. Luke C   |  Thursday, 28 January 2010 at 11:10 am

    This is a fantastic article! Since Apex is my first programming language, exception handling is conceptually new to me and I had not figured out how and when to take advantage of database methods versus DMLs. This will allow much greater sophistication in my code!

    Regarding Governor Limits, although there’s no elegant way of recovery is possible, in one app I calculated how may DMLs and SOQL queries a given process would take and if it exceeded limits, halted execution, sending myself an email with the details so an alternate method could be arranged.

  6. Steve   |  Thursday, 28 January 2010 at 11:14 am

    Glad you liked it Luke! Yep, I think that’s the best way to deal with governor limits.

Leave a Reply