> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/perl6-rosettacode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/perl6-rosettacode/programming_tasks/d/detect_division_by_zero.md).

# Detect division by zero

#### Try/Catch

```perl
sub div($a, $b) {
    my $r;
    try {
        $r = $a / $b;
        CATCH {
            default { note "Unexpected exception, $_" }
        }
    }
    return $r // Nil;
}
say div(10,2);
say div(1, sin(0));
```

#### Output:

```
5
Unexpected exception, Attempt to divide 1 by zero using /
Nil
```

#### Multi Method Dispatch

```perl
multi div($a, $b) { return $a / $b }
multi div($a, $b where { $b == 0 }) { note 'Attempt to divide by zero.'; return Nil }

say div(10, 2);
say div(1, sin(0));
```

#### Output:

```
5
Attempt to divide by zero.
Nil
```
