try {
throw new Error('Whoops!')
} catch (e) {
console.error(e.name + ': ' + e.message)
}
const number = 40;
try {
if(number > 50) {
console.log('Success');
}
else {
// user-defined throw statement
throw new Error('The number is low');
}
// if throw executes, the below code does not execute
console.log('hello');
}
catch(error) {
console.log('An error caught');
console.log('Error message: ' + error);
}
try {
// body of try
throw exception;
}
catch(error) {
// body of catch
}
<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>