# Guard Expressions raising errors?

**URL:** <https://erlangforums.com/t/guard-expressions-raising-errors/1668>\
**Category:** Questions / Help\
**Tags:** guards\
**Created:** [July 26, 2022, 7:09am UTC](https://erlangforums.com/t/guard-expressions-raising-errors/1668 "2022-07-26T07:09:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dominic](https://erlangforums.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@dominic](https://erlangforums.com/u/dominic)\
**Post date:** [July 26, 2022, 7:09am UTC](https://erlangforums.com/t/guard-expressions-raising-errors/1668/1 "2022-07-26T07:09:42Z")

</div>

using `element` for example

in Guards:

```erlang
run(X) when element(1, X) == 1 -> a;
run(_) -> b.

```

call `test(1)` will not raise any error  
in fact it’s `is_tuple(X) andalso element(1, X) == 1`?  
but have a look this .S file

```erlang
run(X) when element(1, X) == 1 -> a;
run(X) -> element(1, X).

```

```erlang
{function, run, 1, 2}.
  {label,1}.
    {line,[{location,"test.erl",5}]}.
    {func_info,{atom,test},{atom,run},1}.
  {label,2}.
    {bif,element,{f,3},[{integer,1},{x,0}],{x,1}}.
    {test,is_eq,{f,3},[{x,1},{integer,1}]}.
    {move,{atom,a},{x,0}}.
    return.
  {label,3}.
    {line,[{location,"test.erl",6}]}.
    {bif,element,{f,0},[{integer,1},{x,0}],{x,0}}.
    return.

```

both `element` call `{bif,element,...}`, `{f,0}` raise error?

in Expr

```erlang
element(1, 1).

```

obvious, raise error

if it’s strange that it (maybe) has a hidden `type check` in guard  
as far as I know it’s undocumented  
and I will write code like these in my work 😅

```erlang
run(Tuple) when is_tuple(Tuple), element(1, Tuple) > 0 -> ...

```

---

<div class="post-metadata">

**Author:** ![Maria-12648430](https://erlangforums.com/user_avatar/erlangforums.com/maria-12648430/32/604_2.png) [@Maria-12648430](https://erlangforums.com/u/Maria-12648430)\
**Post date:** [July 26, 2022, 12:34pm UTC](https://erlangforums.com/t/guard-expressions-raising-errors/1668/2 "2022-07-26T12:34:30Z")

</div>

Errors in guard expressions do not raise an error, that is intended. An error in a guard will simply cause the guard to fail. As documented [here](https://www.erlang.org/doc/reference_manual/expressions.html#guard-expressions) (at the end of the section):

> If an arithmetic expression, a Boolean expression, a short-circuit expression, or a call to a guard BIF fails (because of invalid arguments), the entire guard fails. If the guard was part of a guard sequence, the next guard in the sequence (that is, the guard following the next semicolon) is evaluated.

---

<div class="post-metadata">

**Author:** ![bjorng](https://erlangforums.com/user_avatar/erlangforums.com/bjorng/32/174_2.png) [@bjorng](https://erlangforums.com/u/bjorng)\
**Post date:** [July 26, 2022, 1:27pm UTC](https://erlangforums.com/t/guard-expressions-raising-errors/1668/3 "2022-07-26T13:27:14Z")

</div>

Yes, `{f,0}` means that if the call to `element/2` fails, an exception will be raised. `{f,0}` is used in function bodies.

In guards, `{f,FailureLabel}` is used (where `FailureLabel` is a number greater than 0). If the call fails there will be a jump to `{label,FailureLabel}`.

> [@dominic](#):
>
> and I will write code like these in my work 😅
> 
> ```erlang
> run(Tuple) when is_tuple(Tuple), element(1, Tuple) > 0 -> ...
> 
> ```

I recommend that you don’t add unnecessary `is_tuple/1` guard tests. It will look confusing for other people reading your code and the compiler will currently not optimise away it.

Also, strictly speaking, using `is_tuple(Tuple)` is not sufficient to prevent `element(1, Tuple)` from failing, because `element(1, {})` will also fail.

---

<div class="post-metadata">

**Author:** ![nzok](https://erlangforums.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nzok](https://erlangforums.com/u/nzok)\
**Post date:** [July 27, 2022, 7:55am UTC](https://erlangforums.com/t/guard-expressions-raising-errors/1668/4 "2022-07-27T07:55:35Z")

</div>

Do not say “guard expression”.  
Guards are guards  
and expressions are expressions.  
It _IS_ documented that any guard that  
would have raised an exception had it been an  
expression will simply fail quietly.  
This applies to ALL guards, not just ones  
that involve element(_,_).  
Guards: can only fail, never raise exceptions.  
Expressions: can only raise exceptions, never fail.

---

<div class="post-metadata">

**Author:** ![dischoen](https://erlangforums.com/user_avatar/erlangforums.com/dischoen/32/695_2.png) [@dischoen](https://erlangforums.com/u/dischoen)\
**Post date:** [July 27, 2022, 8:18am UTC](https://erlangforums.com/t/guard-expressions-raising-errors/1668/5 "2022-07-27T08:18:35Z")

</div>

A _guard expression_ is an expression which is allowed in a guard:  
[https://www.erlang.org/doc/reference\_manual/expressions.html#guard-expressions](https://www.erlang.org/doc/reference_manual/expressions.html#guard-expressions)

---

<div class="post-metadata">

**Author:** ![nzok](https://erlangforums.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nzok](https://erlangforums.com/u/nzok)\
**Post date:** [July 27, 2022, 8:44am UTC](https://erlangforums.com/t/guard-expressions-raising-errors/1668/6 "2022-07-27T08:44:36Z")

</div>

Yes, but it’s a dangerously misleading way to talk,  
as witness this thread.
