I've got a Visualforce page that allows the user to fire a batch job manually, using this action method:
public void FireBatch()
{
try
{
Database.ExecuteBatch(new AutobotBatch());
}
catch (System.LimitException e)
{
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.Info, 'There are too many jobs queued to run.'));
}
catch (Exception e)
{
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Oh snap: ' + e.GetMessage()));
}
}
Yet despite the try and catch blocks, if I play the part of an over-keen user and keep hitting the commandButton over and over I get the usual error:
"Attempted to schedule too many concurrent batch jobs"
I know what this means, and I know I could probably count invocations myself; but what I want to do know is why I can't seem to catch this exception. Perhaps the exception is fired in the context of the asynchronous job, but in that case it shouldn't be redirecting the user's browser to the error page.
Anybody know how to catch this and simply display a message without resorting to tracking the jobs manually?