I am using DDMathParser library in my project and I want to use error handling. I want the error to be displayed to user if he inputs wrong expression. What would be my catch block to detect and display related error and its position. Following is my sample code:
do{
var X = "(3 + 4" //Missed Closed Parenthesis
let evaluator = Evaluator()
let expression = try Expression(string: X)
let value = try evaluator.evaluate(expression)
}
catch
{
}
According to DDMathParser it should be Grouping error and by using Range with it it should specify its location where parenthesis got missed or any other error type occurred. Here is its Documentation
You have to cast the error as a GroupedTokenError
to access the specific DDMathParser error messages.
do {
let X = "(3 + 4"
let evaluator = Evaluator()
let expression = try Expression(string: X)
let value = try evaluator.evaluate(expression)
} catch let error as GroupedTokenError {
print(error._code) // 1
print(error.kind) // MissingCloseParenthesis
print(error.range) // 6..<6
} catch let error as NSError {
print(error.debugDescription)
}
Note that you also have to add a generic error catching block for non-DDMathParser errors (in my example a generic ErrorType will be bridged to an NSError to display the contents the Cocoa way).