I'm running into this strange issue regarding the Queueable Apex and Exception handling.

I have something like this: I'm querying for an Email Template(Only one)

List<EmailTemplate> emailTemplate = [Select Id, Subject, HtmlValue, Body from EmailTemplate where DeveloperName =: eTemplate];


if(emailTemplate != null && !emailTemplate.isEmpty()){
    //My logic here

    //Kicking a queuable here to set the field(to TRUE) that fires this functionality
    throw new MyException('something bad happened!');

Now the logic in the else block doesn't fire the queuable jobs.I think the "THROW" statement doesn't let the queuable to fire.

Is there any logic that might help here

  • emailTemplate will never be null.
    – sfdcfox
    Commented Jun 6, 2017 at 21:43

1 Answer 1


Yes, an uncaught exception will cause the entire transaction to roll back, including any executed jobs, DML Operations, etc. You would have to remove your throw if you want the job to fire.

If you want to surface an error in Visualforce, you can just use ApexPages.addMessage or ApexPages.addMessages, depending on what you're most comfortable with. As mentioned in the comments, your code structure may need to change to properly handle the case where your query fails to find a record.

While it's not the only paradigm that works, I find it better to avoid exceptions in your execution path where possible. The common approach is to query to a list and check if it is empty.

List<EmailTemplate> candidates = [/*query*/];
if (candidates.isEmpty())
    ApexPages.addMessage(new ApexPage.Message(
        ApexPages.Severity.FATAL, 'No template found'
    // execute job
  • Yeah ..it worked fine without the throw exception....the reason I'm using the throw statement is to show the exception on our UI(VF with ExtJs) which is a combination of almost 10 objects.I'm trying to figure out if there is any alternate way
    – sai kumar
    Commented Jun 6, 2017 at 21:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .