# StaticCompiler - how to handle integer division error?

**URL:** <https://discourse.julialang.org/t/staticcompiler-how-to-handle-integer-division-error/108061>\
**Category:** General Usage\
**Tags:** exception, staticcompiler\
**Created:** [December 26, 2023, 3:30pm UTC](https://discourse.julialang.org/t/staticcompiler-how-to-handle-integer-division-error/108061 "2023-12-26T15:30:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [December 26, 2023, 3:30pm UTC](https://discourse.julialang.org/t/staticcompiler-how-to-handle-integer-division-error/108061/1 "2023-12-26T15:30:28Z")

</div>

StaticCompiler.jl does not allow exceptions. I got stuck trying to compile code involving the mod operator `%`, as it can raise an exception `DivideError` if you’re dividing by zero. How can I work around this?

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [December 26, 2023, 3:41pm UTC](https://discourse.julialang.org/t/staticcompiler-how-to-handle-integer-division-error/108061/2 "2023-12-26T15:41:29Z")

</div>

I found a solution. Define a new function

```julia
function myrem(a, b)
    b==0 ? 0 : a%b
end

```

Then replace any `a%b` in the code by `myrem(a, b)`. This allows the compiler to know that I never call `%` when `b` is zero, and I’m able to use `StaticCompiler.compile_executable`, without getting errors.

Edit: corrected operator name from `mod` to `rem`.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 27, 2023, 3:17am UTC](https://discourse.julialang.org/t/staticcompiler-how-to-handle-integer-division-error/108061/3 "2023-12-27T03:17:18Z")

</div>

You can redefine `%` within your module if needed. Also you may mean `rem` rather than `mod`.

```julia
julia> %(a,b) = b == 0 ? 0 : Base.rem(a, b)
% (generic function with 1 method)

```
