JavaScript error



try statement tests the code block for errors.

catch Statement processing error.

throw statement creates a custom error.


JavaScript Errors

When the JavaScript engine executes JavaScript code, various errors will occur.

may be a syntax error, usually a coding error or typo made by the programmer.

could be a spelling mistake or a missing feature in the language (possibly due to browser differences).

The error may be caused by incorrect output from the server or user.

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


JavaScript throws errors

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

The technical term to describe this situation is: JavaScript will throw an error.


JavaScript try and catch

try statements allow us to define blocks of code that are tested for errors when executed. The

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

JavaScript statements try and catch appear in pairs.

Syntax

try {
	//在这里运行代码
} catch(err) {
	//在这里处理错误
}

Examples

In the following example, we intentionally wrote a typo in the code of the try block.

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

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
<script>
var txt="";
function message(){
	try {
		adddlert("Welcome guest!");
	}
	catch(err) {
		txt="本页有一个错误。\n\n";
		txt+="错误描述:" + err.message + "\n\n";
		txt+="点击确定继续。\n\n";
		alert(txt);
	}
}
</script>
</head>
<body>

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

</body>
</html>

Run instance»

Click the "Run instance" button to view the online instance


Throw Statement

The throw statement allows us to create custom errors.

The correct technical term is: create or throw an exception (exception).

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

Syntax

##throw
exception
Exceptions can be JavaScript strings, numbers, and logical values or object.

Example

This example detects the value of the input variable. If the value is wrong, an exception (error) is thrown. catch will catch this error and display a custom error message:

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
<script>
function myFunction(){
	try{ 
		var x=document.getElementById("demo").value;
		if(x=="")    throw "值为空";
		if(isNaN(x)) throw "不是数字";
		if(x>10)     throw "太大";
		if(x<5)      throw "太小";
	}
	catch(err){
		var y=document.getElementById("mess");
		y.innerHTML="错误:" + err + "。";
	}
}
</script>
</head>
<body>

<h1>我的第一个 JavaScript</h1>
<p>请输出一个 5 到 10 之间的数字:</p>
<input id="demo" type="text">
<button type="button" onclick="myFunction()">测试输入</button>
<p id="mess"></p>

</body>
</html>

Running Instance»Click the "Run Instance" button to view the online instance


Please note that the above example will also throw an error if there is an error in the getElementById function.