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
parkerAPTparkerAPT 

Can you catch a "Too many query rows" Exception ?

Is it possible to catch the exception thrown when there are too many query rows?

The following code does not catch any exception when there are more than 10,000 rows (500 in testCase) returned.

In this case, because it is a testMethod, the exception gets thrown because there are more than 500 rows returned.


static testMethod void testCatchTooManyRows(){
Boolean caught = false;
try{
Integer theCount = [SELECT count() from Contact];
}catch(Exception e){
caught = true;
}
System.assert(caught);
}
andresperezandresperez

The system does not catch any errors thrown because there are no erros to be caught...

You are selecting an Integer that represents the number of rows to be retrieved. If you want to fire the exception try changing your code to something like this:

Code:
static testMethod void testCatchTooManyRows(){
   Boolean caught = false;
   try{
      List<Contact_c> MyRecords = [SELECT count() from Contact];
   } catch(Exception e){
      caught = true;
   }
   System.assert(caught);
}
parkerAPTparkerAPT
Andre,

Your code does not compile.

The return type of "[SELECT count() from Contact]" is of type Integer, not List
andresperezandresperez
Sorry... My mistake.
 
This is what I meant:
 
static testMethod void testCatchTooManyRows(){
   Boolean caught = false;
   try{
      List<Contact_c> MyRecords = [SELECT ID from Contact];
   } catch(Exception e){
      caught = true;
   }
   System.assert(caught);
}

 
ThomasTTThomasTT
I have the exact same problem. Could somebody answer this? or is it a bug?
ThomasTTThomasTT

It seems that we can't catch these kinds of exception...

 

http://community.salesforce.com/sforce/board/message?board.id=apex&thread.id=7382