You may be looking for try-catch:
try {
alert(foo);
} catch(e) {
alert('The code got the following error: '+e.message);
}
Whenever the code between try {}receives an error, a block will be executed catch(e) {}, and the argument eis the object of the error for the error that occurred. In this case, the variable is foonot defined, so the execution of this code will result in the warning message "The code received the following error: foo not defined"
source
share