I’m making a calculator in Javascript for my school homework and it’s using the function eval(). Yes, I know, eval is evil, but I can assure you that I already secured this, so there’s no way of exploiting it.
The eval turns the value in textbox into an answer, which is then displayed in another textbox. However, when the syntaxe is wrong (for example, user enters “1++2”) I would like if the script displayed some kind of error. But eval() just seems to disappear when the input is unvalid. It returns no value, no error (well, chrome tries explaining it with ‘Uncaught Syntaxerror’, but that is no use for me) so I have no way to explain to the script what to do, if user messes up the syntaxe.
TL;DR: How do I make the script display an error message, if the eval() has unvalid input?
Thanks in advance
It
throws (raises) an exception, which you cancatch(handle) and do whatever you want with: