---
schema_version: "1"
id: 49164c3918fc8464824a9690d52ee92eb3ab17707ac7f9be10c0ddd2199dad12
title: TemplateApexException Apex Examples
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/analytics/bi_dev_guide_wave_templates/guides/reference/bi-templatesdev-apex-class-error-examples.md
canonical_url: https://developer.salesforce.com/docs/analytics/bi-dev-guide-wave-templates/guide/bi-templatesdev-apex-class-error-examples.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 637091c0e3f444de2d7029e7acde577db9a1bfa35384ebeb18147ab6226303a0
products:
  - Analytics Templates Developer Guide
  - analytics
---

# TemplateApexException Apex Examples

Use the `TemplateApexException` class to handle errors cleanly and provide clear information to template users.

**note:** Throwing an exception fails the configuration wizard. You can also create variables to track errors and report them to the user in the wizard without failing.

## Report an error to the user with a TemplateApexException:

```nolang
public override void onConfigurationRetrieval(wavetemplate.WaveTemplateInfo template)
{
    // Run a query if Forecast History is enabled and report failure if isn't
    List<User> usersWithForecast = Database.query('SELECT Id FROM User WHERE ForecastEnabled = true LIMIT 1');
    if (usersWithForecast.isEmpty()) {
        // Report issue to user
        throw new TemplateApexException('Forecast history is not enabled. Please have your admin enable it.');
    }
}
```

## Handle database exceptions with a TemplateApexException

```nolang
public override void onConfigurationRetrieval(wavetemplate.WaveTemplateInfo template)
{
     String query = 'SELECT DeveloperName, MasterLabel, SplitField, SplitEntity FROM OpportunitySplitType WHERE IsActive = true AND SplitEntity = \'Opportunity\'';
     try {
         List<SObject> activeSplitTypes = Database.query(query);
         if (activeSplitTypes.isEmpty()) {
             template.getVariables().get('Has_OpportunitySplits').setComputedValue('No');
         }
         else {
             template.getVariables().get('Has_OpportunitySplits').setComputedValue('Yes');
         }
      }
      catch (Exception e) {
          throw new TemplateApexException('Database query to determine Opportunity Splits failed: ' + e.getMessage());
      }
}
```
