function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rohit SharmaGRohit SharmaG 

LimitException Exception In Apex

How to handle LimitException in Apex :

try {
//Dynamic SOQL builder
String soql= QueryBuilder+' '+cat1+' '+cat2+ ' '+cstatus+' '+reqCntry+' '+agentCntry+' '+empCntry+' '+reqFstName+' '+reqLstName+' '+reqstUPI+' '+mngUPI+' '+mngName+' '+csOwner;

  //Retrieve the Cases
  cases = Database.query(soql);
    system.debug('###Case search list '+cases);
    system.debug('@@@Case search size '+cases.size());
                 if(cases.size()> 0){
                           displayresult = true;
                           displayresult2 = false;
                         displayresult = false;
                          displayresult2 = true;
                          msg ='No records to display';
    } catch (Exception e) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, ''+e));
you can catch it using genric exception class or by using limitexception class.
Deepak Kumar ShyoranDeepak Kumar Shyoran
If you're hitting the SOQL record retrieve limit then you have to put some Limit or filter in your Query.

As described below
String query =  'Select id,Name ,Some_Other_Fields__c  from Object_Name__c limit 10000 ' ;

There is no way to handle limit exception. You can not catch these exception in try catch block. 

But Apex has provided Limit class. Limit class can be used to know limit is reached or not.

Look at the following post: