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
SalesRedSalesRed 

Try Catch For Database.getQueryLocator(query) Exception

Hello,

I have the following code in an Apex Batch Apex Class.

try{
            return Database.getQueryLocator(query);
        }catch(Exception ex){
            return null;
        }

However I cannot seem to catch an exception if an invalid query is provided. I receive the following error
System.UnexpectedException: Start did not return a valid iterable object..

If I try to catch the System.UnexpectedException I still receive an error.

Should a try/catch not work in this scenario?

Thanks in advance for any help.

Best Answer chosen by SalesRed
Henry AkpalaHenry Akpala
Ok, I think this is your problem.  The Start method expects to return a valid iterable object.  So you need to write the error to a system debug log and then return a stack iteratable object.
try{
   return iterable object;//this is correct with your existing code
}catch(Exception x){
   System.debug(Error Message);
   return iterable object;
}

All Answers

Henry AkpalaHenry Akpala
Can you post the query that is passed to the batch apex class?
SalesRedSalesRed
I was trying to test a failing SOQL statememt therefore I just passed in "NULL" as the querystring assuming it would be caught in the try/catch.

Thanks for your help.
Henry AkpalaHenry Akpala
Ok, I think this is your problem.  The Start method expects to return a valid iterable object.  So you need to write the error to a system debug log and then return a stack iteratable object.
try{
   return iterable object;//this is correct with your existing code
}catch(Exception x){
   System.debug(Error Message);
   return iterable object;
}

This was selected as the best answer
SalesRedSalesRed
Hi Thanks for your help. You are correct I had to return an iterable object!