# How to raise an exception in DBT macro

**URL:** <https://discourse.getdbt.com/t/how-to-raise-an-exception-in-dbt-macro/12082>\
**Category:** Help\
**Created:** [February 16, 2024, 7:50am UTC](https://discourse.getdbt.com/t/how-to-raise-an-exception-in-dbt-macro/12082 "2024-02-16T07:50:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pragya.yadav](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@pragya.yadav](https://discourse.getdbt.com/u/pragya.yadav)\
**Post date:** [February 16, 2024, 7:50am UTC](https://discourse.getdbt.com/t/how-to-raise-an-exception-in-dbt-macro/12082/1 "2024-02-16T07:50:05Z")

</div>

## I am converting pl sql code to dbt. This code validates data based on certain rules and then raises exception if it fails.

## I understand we can’t raise exceptions in dbt models, so i have created a macro and am using exceptions.raise\_compiler\_error within if block but its giving syntax error.

## Code Snippet, If condition is giving error -

{% macro test\_scenarios\_macro(input\_number) %}

{% set sql\_statement %}  
select count(1)  
from table1  
where column1 = ‘{{input\_number}}’  
{% endset %}

{%- set lkp\_EMIS\_SOURCE\_ID = dbt\_utils.get\_single\_value(sql\_statement, default=“‘NULL’”) -%}

{% if {{lkp\_EMIS\_SOURCE\_ID}} == 6 %}  
{{ exceptions.raise\_compiler\_error(“WRONG INPUT”) }}  
{% endif %}

{% endmacro %}

---

<div class="post-metadata">

**Author:** ![brunoszdl](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/brunoszdl/32/4113_2.png) [@brunoszdl](https://discourse.getdbt.com/u/brunoszdl)\
**Post date:** [February 16, 2024, 2:53pm UTC](https://discourse.getdbt.com/t/how-to-raise-an-exception-in-dbt-macro/12082/2 "2024-02-16T14:53:05Z")

</div>

try removing {{ }}

such as

`{% if lkp_EMIS_SOURCE_ID == 6 %}`

---

<div class="post-metadata">

**Author:** ![pragya.yadav](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@pragya.yadav](https://discourse.getdbt.com/u/pragya.yadav)\
**Post date:** [February 20, 2024, 6:55am UTC](https://discourse.getdbt.com/t/how-to-raise-an-exception-in-dbt-macro/12082/3 "2024-02-20T06:55:33Z")

</div>

Thanks, that worked for me. Now I am getting same error for no equal to condition.

{% if lkp\_EMIS\_SOURCE\_ID != 6 %}  
{{ exceptions.raise\_compiler\_error(“WRONG INPUT”) }}  
{% endif %}

Also, can you please help me to understand where does dbt captures this exception. Can I provide my custom schema/ table to capture exceptions.
