# Try() expression doesn't catch dividing by zero?

**URL:** <https://forum.inductiveautomation.com/t/try-expression-doesnt-catch-dividing-by-zero/76149>\
**Category:** Ignition\
**Created:** [July 10, 2023, 6:09pm UTC](https://forum.inductiveautomation.com/t/try-expression-doesnt-catch-dividing-by-zero/76149 "2023-07-10T18:09:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cole\_thorthon](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@cole\_thorthon](https://forum.inductiveautomation.com/u/cole_thorthon)\
**Post date:** [July 10, 2023, 6:09pm UTC](https://forum.inductiveautomation.com/t/try-expression-doesnt-catch-dividing-by-zero/76149/1 "2023-07-10T18:09:43Z")

</div>

try(4/0,99)

Is there anyway to stop dividing by zero errors?

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [July 10, 2023, 6:12pm UTC](https://forum.inductiveautomation.com/t/try-expression-doesnt-catch-dividing-by-zero/76149/2 "2023-07-10T18:12:48Z")

</div>

> [@Expression Divide By 0](https://forum.inductiveautomation.com/t/expression-divide-by-0/21434/5):
>
> try() wraps the expression evaluation in a try/catch and if an exception is caught uses the bailout value. Divide by None will throw an exception, divide by 0 is infinity, no exception thrown. Seems reasonable to me!

---

<div class="post-metadata">

**Author:** ![cole\_thorthon](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@cole\_thorthon](https://forum.inductiveautomation.com/u/cole_thorthon)\
**Post date:** [July 10, 2023, 6:14pm UTC](https://forum.inductiveautomation.com/t/try-expression-doesnt-catch-dividing-by-zero/76149/3 "2023-07-10T18:14:58Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/a/faaf7347ba4daae24d29a66e0e5058faf08b6a2f.png)

dividing by zero does throw an exception tho and the try function isn't catching it on the function above

this try(4/None,99) does render the failback value

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [July 10, 2023, 6:16pm UTC](https://forum.inductiveautomation.com/t/try-expression-doesnt-catch-dividing-by-zero/76149/4 "2023-07-10T18:16:15Z")

</div>

Divide by `None` will throw an exception, divide by `0` is infinity, no exception thrown.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 10, 2023, 6:16pm UTC](https://forum.inductiveautomation.com/t/try-expression-doesnt-catch-dividing-by-zero/76149/5 "2023-07-10T18:16:29Z")

</div>

No, dividing by zero is supported fine _in the expression evaluation_:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/1/c1b5f39ca9fc81557594df5b2a53c93109d64b0a.png)

The type conversion error is happening when the _tag system_ receives the infinity from the expression and cannot coerce it to a value to apply to the tag. As in the link @jlandwerlen posted above.

---

<div class="post-metadata">

**Author:** ![cole\_thorthon](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@cole\_thorthon](https://forum.inductiveautomation.com/u/cole_thorthon)\
**Post date:** [July 10, 2023, 6:23pm UTC](https://forum.inductiveautomation.com/t/try-expression-doesnt-catch-dividing-by-zero/76149/6 "2023-07-10T18:23:59Z")

</div>

```auto
abs(({[.]HL} - {[.]LL)) / (({[.]HL} + {[.]HL}) / 2) * 100

```

Maybe I can ask for more pointed help. I still want to render a percentage difference of '0' when both the HL and LL tag are zeros. Any ideas?

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [July 10, 2023, 6:28pm UTC](https://forum.inductiveautomation.com/t/try-expression-doesnt-catch-dividing-by-zero/76149/7 "2023-07-10T18:28:56Z")

</div>

Use `if()` as demonstrated in the link posted above. Also see the docs.  
[https://docs.inductiveautomation.com/display/DOC81/if](https://docs.inductiveautomation.com/display/DOC81/if)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [July 10, 2023, 7:12pm UTC](https://forum.inductiveautomation.com/t/try-expression-doesnt-catch-dividing-by-zero/76149/8 "2023-07-10T19:12:34Z")

</div>

```auto
if(!{[.]HL} && !{[.]LL}, 0, abs(({[.]HL} +{[.]LL}) / 2) * 100))

```

---

<div class="post-metadata">

**Author:** ![cole\_thorthon](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@cole\_thorthon](https://forum.inductiveautomation.com/u/cole_thorthon)\
**Post date:** [July 10, 2023, 8:30pm UTC](https://forum.inductiveautomation.com/t/try-expression-doesnt-catch-dividing-by-zero/76149/9 "2023-07-10T20:30:09Z")

</div>

```auto
if(!{[.]HL} && !{[.]LL}, 0, abs(({[.]HL} +{[.]LL}) / 2) * 100)

```
