diff options
author | Tom Willemse | 2020-11-11 22:01:10 -0800 |
---|---|---|
committer | Tom Willemse | 2020-11-11 22:01:10 -0800 |
commit | 006836553321d5ce25ff4de4b5a71f19606fec37 (patch) | |
tree | 2ae75d26d49b32014b0af0a75698c98a4155d85d /src/com/craftinginterpreters | |
parent | 5ff2f8be5aa459f0a7365e63d633e9976c6ed926 (diff) | |
download | crafting-interpreters-006836553321d5ce25ff4de4b5a71f19606fec37.tar.gz crafting-interpreters-006836553321d5ce25ff4de4b5a71f19606fec37.zip |
Throw a runtime error on division by zerochapter-7-challenges
Diffstat (limited to 'src/com/craftinginterpreters')
-rw-r--r-- | src/com/craftinginterpreters/lox/Interpreter.java | 9 |
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; |