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
Park Walker (TAGL)Park Walker (TAGL) 

Catch fails to catch Exception

I have a VisualPage controller which attempts to schedule an APEX task using the scheduler. It works fine as long as the job is not currently scheduled, but I am trying to catch the exception thrown when it already exists. The exception is not being caught and ends up being displayed to the user. The line displayed in the error message is the line number of the schedule call.



private ApexPages.Message ScheduleJob() { ApexPages.Message msg; String jt = '0 0 ' + cfg.Time_of_Day__c + ' * * ?'; String jn = 'BatchUpdater'; BatchUpdateRunner jo = new BatchUpdateRunner(); try { System.schedule(jn, jt, jo); msg = new ApexPages.Message(ApexPages.Severity.INFO,'The job was successfully scheduled.'); } catch (System.Exception ex) { String m = ex.getMessage(); if (m.contains('already scheduled for execution')) m += ' You may need to delete the existing job under Admin->Monitoring->Scheduled Jobs.'; msg = new ApexPages.Message(ApexPages.Severity.ERROR, m); } return msg; }




Park Walker (TAGL)Park Walker (TAGL)

I opened a case for this issue and was told the the exception being thrown was uncatchable. It's the first time I've heard that there is such a class of exceptions. It would be great if they were documented.


I am still looking for a way to determine if an Apex class has a scheduled job. The AsynchApexJob class will not return a record until the job is submitted and the CronTrigger is also not available until that time. It appears that there is no way to look into the queue.