How to display alert after error of an eval ()?

3

I have a calculator that works something like this:

[bolas][+][preco da bola]

I scan the string by replacing [XXXXXXX] with the corresponding value and use eval to perform the calculation. But there may be cases of error when for example the calculation looks like this:

eval("5 + [preco da bola]");
  • The JS code for when this error occurs?
  • If the code stops, can I prevent this so that it keeps running?
  • Can I detect the error and send a custom alert?
asked by anonymous 06.02.2015 / 20:09

2 answers

2

Would that be? ....

try{
    eval("5 + [preco da bola]");
}catch(e){
    console.log(e);
}
    
06.02.2015 / 20:12
2

You can always make a mistake by not going to the client side with:

try{
    // o código que pode dar erro aqui
}
catch(erro){
    // este codigo é corrido quando houver erros. A variável "erro" contêm o código do erro
}

I think it would be more interesting to do this without eval, if you want help you have to give more details.

    
06.02.2015 / 20:14