I am getting a SOQL System.LimitException. I know there is a query within a for loop. But it's my work, it needs to be there. What i wanted to do is handle this exception in apex controller like

  if exception; 
}catch(Exception e) {
  set massage here;

Can i achieve something like this?


  • 5
    Governor limit exceptions can't be caught in Apex so you can't handle them. Instead you must organise your code to avoid them. If you explain more about what you are trying to do someone might be able to suggest a "bulkification" pattern that would work. – Keith C Aug 11 '15 at 8:21
  • @Mihai, Thanks for editing and formatting the question. – Prabhat Ranjan Aug 11 '15 at 8:30
  • 5
    I'd be surprised if the query actually needs to be in a loop. If you are willing to post what you have there's a good chance someone can show you how to move it out. – martin Aug 11 '15 at 8:33

You cannot catch Limit exceptions.

The best you can do is use the Limits methods to test when you are approaching the limits and act accordingly (for example stop further processing and record an error).


For example

if (Limits.getLimitQueries() > 0) {
  //do your query
} else {
  //handle this error situation
  • Thanks for providing link.However on opening this link it show error 404 i.e page not found. – Prabhat Ranjan Aug 12 '15 at 4:44
  • 1
    I've updated the link – Doug B Aug 12 '15 at 9:12

No there is no such way to catch Limit exceptions in try catch. All other exception can be caught in try catch but not related to limit exceptions.


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.