From 505f428029b02e992c05491713e9ef96d2a2d8fc Mon Sep 17 00:00:00 2001 From: Oliver Hummel Date: Thu, 14 Dec 2023 08:54:48 +0100 Subject: [PATCH] Added a simple exception handling for division by zero attempts. --- .../informatik/spreadsheet/Spreadsheet.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index 4504b06..de46be0 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -126,8 +126,13 @@ public class Spreadsheet { result = "TODO"; // TODO else if (formula.startsWith("MAX(")) // e.g. MAX(A1:A10) -> Standardabweichung result = "TODO"; // TODO - else if (!formula.isEmpty()) - result = "" + calculate(formula); + else if (!formula.isEmpty()) { + try { + result = "" + calculate(formula); + } catch(ArithmeticException ae) { + result = "exc."; + } + } cells[row][col].setValue("" + result); } @@ -153,7 +158,7 @@ public class Spreadsheet { * @param formula The expression to be evaluated. * @return The result calculated. */ - private long calculate(String formula) { + private long calculate(String formula) throws ArithmeticException { Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula); long res = 0;