try {
// body of try
throw exception;
}
catch(error) {
// body of catch
}
<p id="error"></p>
<script>
try {
wrongalert("Welcome guest!");
}
catch(err) {
document.getElementById("error").innerHTML = err.message;
}
</script>
<body>
<div id="error"> </div>
<script>
try{
if(x==1)
{
throw "x must not be 1";
}
}
catch(err)
{
document.getElementById("error").innerHTML = err;
}
</script>
</body>
let json = '{ "age": 30 }'; // incomplete data
try {
let user = JSON.parse(json); // <-- no errors
alert( user.name ); // no name!
} catch (e) {
alert( "doesn't execute" );
}