1

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

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

Can i achieve something like this?

thanks..

3
  • 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
    Commented Aug 11, 2015 at 8:21
  • @Mihai, Thanks for editing and formatting the question. Commented Aug 11, 2015 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
    Commented Aug 11, 2015 at 8:33

2 Answers 2

3

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).

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_limits.htm

For example

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

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.

3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .