<script type="text/javascript">
<!--
try
{
//var my_sum=5;
document.write("The sum of 2 and 3 = " + my_sum);
}
catch(err)
{
document.write("There is an error Sorry");
document.write("<br>...You can continue with rest of the page...<br>");
}
//-->
</script>
We can display the error message which has caused the exception by error object. Here internet explorer and firefox behaves differently so we have to identify the browsers and then display the message accordingly through an alert box.
<script type="text/javascript">
<!--
try
{
//var my_sum=5;
document.write("The sum of 2 and 3 = " + my_sum);
}
catch(err)
{
document.write("There is an error Sorry");
if(navigator.userAgent.indexOf("Firefox")!=-1){
var msg=err;
}
else if (navigator.appName.indexOf("Internet Explorer")!=-1){
var msg=err.description;
}
alert(msg);
}
//-->
</script>
throw
Statement in JavaScriptThe throw
statement in JavaScript is used to generate custom error messages in our program. When a throw
statement is encountered, it stops the execution of the code and passes control to the nearest catch
block, if available, or displays the error in the console. We can throw any type of value—an error object, string, number, or other data types—to indicate that something went wrong in our code.
throw expression;
throw
to generate an error when user input is invalid.throw
can be used in conjunction with try...catch
to handle errors gracefully.function divideNumbers(a, b) {
if (b === 0) {
throw "Division by zero is not allowed";
}
return a / b;
}
try {
let result = divideNumbers(10, 0);
console.log(result);
} catch (error) {
console.log("Error: " + error);
}
throw "Division by zero is not allowed";
: When b
is 0, this custom error message is thrown.try...catch
block: The catch
block catches the thrown error, preventing the script from crashing, and displays a meaningful error message.Error: Division by zero is not allowed
We can throw other data types such as:
Error
, RangeError
, TypeError
, etc.function checkAge(age) {
if (age < 18) {
throw new Error("You must be at least 18 years old.");
}
console.log("You are old enough.");
}
try {
checkAge(16);
} catch (error) {
console.log(error.name + ": " + error.message);
}
throw new Error("message")
: Throws a predefined Error
object with a custom message.error.name
: The type of error (Error
).error.message
: The custom error message passed during the throw
.Error: You must be at least 18 years old.
Error
: A generic error object.TypeError
: Thrown when a value is not of the expected type.RangeError
: Thrown when a number is out of range.ReferenceError
: Thrown when an invalid reference is made (e.g., using an undefined variable).SyntaxError
: Thrown when there is a syntax issue in our code.function checkNumber(num) {
if (typeof num !== 'number') {
throw new TypeError("Expected a number");
}
if (num < 0 || num > 100) {
throw new RangeError("Number must be between 0 and 100");
}
console.log("Valid number: " + num);
}
try {
checkNumber(150);
} catch (error) {
console.log(error.name + ": " + error.message);
}
RangeError: Number must be between 0 and 100