Throw a runtime error on division by zero
This commit is contained in:
parent
5ff2f8be5a
commit
0068365533
1 changed files with 7 additions and 2 deletions
|
@ -119,9 +119,14 @@ class Interpreter implements Expr.Visitor<Object> {
|
|||
}
|
||||
|
||||
throw new RuntimeError(expr.operator, "Operands must be two numbers or two strings.");
|
||||
case SLASH:
|
||||
case SLASH: {
|
||||
checkNumberOperands(expr.operator, left, right);
|
||||
return (double) left / (double) right;
|
||||
double rhs = (double) right;
|
||||
if (rhs == 0) {
|
||||
throw new RuntimeError(expr.operator, "Division by zero.");
|
||||
}
|
||||
return (double) left / rhs;
|
||||
}
|
||||
case STAR:
|
||||
checkNumberOperands(expr.operator, left, right);
|
||||
return (double) left * (double) right;
|
||||
|
|
Loading…
Reference in a new issue