Division by Zero¶
Division by zero is a runtime error in Desi. Both integer and float division by zero cause the program to panic.
Behavior¶
When dividing by zero:
1. Error message printed to stderr: panic: integer division by zero
2. Program exits with code 1
Why Panic?¶
Unlike some languages that return inf or NaN for float division by zero, Desi treats all division by zero as an error because:
- Safety: Silent corruption (returning 0 or
inf) can cause bugs that are hard to track down - Consistency: Same behavior for int and float makes the language predictable
- Explicitness: Errors should be visible, not hidden
Safe Division¶
Check before dividing:
def safe_divide(a: int, b: int) -> int:
if b == 0:
print("Cannot divide by zero", file=sys.stderr)
return 0
return a / b
Future: Result Types¶
In future versions, Desi may offer a checked_div function. This is a sketch,
not working code:
let result = checked_div(10, 0) # Returns Result<int, DivError>
let msg = match result:
Result.Ok(v): str(v)
Result.Err(e): e
print(msg)
See Also¶
- Error Handling - For Result types