Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JavaScript error handling Throw, Try, and Catch


May 06, 2021 JavaScript


Table of contents


JavaScript errors - throw, try, and catch


The try statement tests for errors in the block of code.

The catch statement handles the error.

The throw statement creates a custom error.


JavaScript error

When the JavaScript engine executes JavaScript code, various errors occur:

It can be a syntax error, usually a coding error or typo caused by a programmer.

This could be a spelling error or missing functionality in the language (possibly due to browser differences).

The error may have been caused by an error output from the server or user.

Of course, it may also be due to many other unpredictable factors.


JavaScript throw error

When an error occurs, when something goes wrong, the JavaScript engine usually stops and generates an error message.

The technical term for describing this situation is that JavaScript throws an error.


JavaScript try and catch

Try statements allow us to define blocks of code that are tested incorrectly at execution time.

The catch statement allows us to define the block of code that is executed when an error occurs in the try block.

JavaScript statements try and catch appear in pairs.

Grammar

try
{
// Run the code here
}
catch(err)
{
/ / Treatment error here
}

In the following example, we deliberately wrote a typo in the try block's code.

The catch block catches the error in the try block and executes code to handle it.

<!DOCTYPE html>
<html>
<head>
<script>
var txt="";
function message()
{
try
{
adddlert("Welcome guest!");
}
catch(err)
{
TXT = "This page has an error. \ n \ n";
TXT + = "Error Description:" + Err.Message + "\ n \ n";
TXT + = "Click OK to continue. \ n \ n";
alert(txt);
}
}
</script>
</head>

<body>
<input type="button" value="查看消息" onclick="message()">
</body>

</html>

Try it out . . .


Throw statement

The throw statement allows us to create custom errors.

The correct technical term is to create or throw an exception.

If you use throw with try and catch, you can control the program flow and generate custom error messages.

Grammar

throw exception

Exceptions can be JavaScript strings, numbers, logical values, or objects.

This example detects the value of the input variable. I f the value is wrong, an exception (error) is thrown. Catch catches the error and displays a custom error message:

<script>
function myFunction()
{
try
{
var x=document.getElementById("demo").value;
if(x=="") throw "empty";
if(isNaN(x)) throw "not a number";
if(x>10) throw "too high";
if(x<5) throw "too low";
}
catch(err)
{
var y=document.getElementById("mess");
y.innerHTML="Error: " + err + ".";
}
}
</script>

<h1>My First JavaScript</h1>
<p>Please input a number between 5 and 10:</p>
<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="mess"></p>

Try it out . . .

Note that if the getElementById function goes wrong, the example above also throws an error.

Related articles

JavaScript Standard Reference Manual: JavaScript Error Handling Mechanism