How to catch an error inside the expression and avoid software to crash #289
-
I am using EvalEx in my Android calculator app that I am currently developing. But if user writes any expression that is incorrect (like, for example, "3+*5"), the app crashes and gets closed because obviously, EvalEx cannot solve incorrect expressions. But I would like my app not to crash every time user makes a mistake, I want my app to show an error (like any other calculator app does) instead, but first of all, I have to know how to detect errors. How could I do that? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Do as you do with any other code in Java: Check for exceptions: Expression e = new Expression("3+*5)");
try {
BigDecimal result = e.eval();
} catch (ExpressionException ex) {
System.out.print("Error: " + ex.getMessage());
} or if you want to catch everything: This will print out |
Beta Was this translation helpful? Give feedback.
Do as you do with any other code in Java: Check for exceptions:
or if you want to catch everything:
catch (Exception ex)
This will print out
Error: Unknown unary operator * at character position 3