summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom Willemse2020-11-11 22:01:10 -0800
committerGravatar Tom Willemse2020-11-11 22:01:10 -0800
commit006836553321d5ce25ff4de4b5a71f19606fec37 (patch)
tree2ae75d26d49b32014b0af0a75698c98a4155d85d
parent5ff2f8be5aa459f0a7365e63d633e9976c6ed926 (diff)
downloadcrafting-interpreters-chapter-7-challenges.tar.gz
crafting-interpreters-chapter-7-challenges.zip
Throw a runtime error on division by zerochapter-7-challenges
-rw-r--r--src/com/craftinginterpreters/lox/Interpreter.java9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/com/craftinginterpreters/lox/Interpreter.java b/src/com/craftinginterpreters/lox/Interpreter.java
index 57e697c..1d87b87 100644
--- a/src/com/craftinginterpreters/lox/Interpreter.java
+++ b/src/com/craftinginterpreters/lox/Interpreter.java
@@ -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;