Detecting governor limits through apex

Is it possible to programatically detect if you have reached a governor limit?

For example, you can make 200 future calls per license per 24 hours. IF you hit that limit SFDC just starts to blow up on you. Is there some way to determine if you are under/over on the limit so you can react accordingly?

I’d prefer not performing an action if I’m over the limit rather than catching the exception and continuing to try in subsequent calls as it’s a bit inefficient.

Answer

First of all, the exception thrown by hitting a limit, System.LimitException is uncatchable and means that your script will be killed, even if it happens inside a try/catch block. There is a class, Limits, that contains a number of static methods that allow you to check your governor limit consumption, see: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_limits.htm

With that said, your example of @future calls per day is one of the limits that simultaneously is and isn’t a governor limit as I believe it throws a System.AsyncException instead which is catchable. is not catchable, and kills your script as a LimitException would.

Attribution
Source : Link , Question Author : Ryan Elkins , Answer Author : ca_peterson

Leave a Comment