Skip to content

How to catch an error inside the expression and avoid software to crash #289

Answered by uklimaschewski
Megabyte94 asked this question in Q&A
Discussion options

You must be logged in to vote

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: catch (Exception ex)

This will print out Error: Unknown unary operator * at character position 3

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Megabyte94
Comment options

Answer selected by uklimaschewski
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants