This is the 31st day of my participation in the August More Text Challenge.

  • Various errors occur when executing JavaScript code.

  • Errors can be coding errors by programmers, errors caused by incorrect input, or other unforeseen problems

  • The try statement enables you to test for errors in a code block

  • The catch statement allows you to handle errors

  • Finally allows you to execute code after a try and catch, no matter what the result

  • The throw statement allows you to create custom errors

Try and catch

The try statement allows you to define a block of code to detect errors at execution time

The catch statement allows you to define a block of code to execute if an error occurs in the try block

JavaScript statements try and catch come in pairs:

try{block of code for testing}catch(err) {block of code that handles errors}Copy the code

Finally statement

The finally statement allows you to execute code after a try and catch, regardless of the result:

try{block of code for testing}catch(err) {block of code that handles errors}finally{no mattertry / catchThe result is how to execute the code block}Copy the code
<! DOCTYPEhtml>
<html>          
 <body>
<p>Please enter a number between 5 and 10:</p>
<input id="demo" type="text">
<button type="button" onclick="myFunction()">Detection of the input</button>
<p id="p01"></p>
<script>
function myFunction() {
    var message, x;
    message = document.getElementById("p01");
    message.innerHTML = "";
    x = document.getElementById("demo").value;
      try {
       if (x == "") throw "It's empty";
       if (isNaN(x)) throw "Not a number.";
       x = Number(x);
       if (x > 10) throw "Too much";
       if (x < 5) throw "Too small";
        } catch (err) {
           message.innerHTML = "Input: + err;
        } finally {
          document.getElementById("demo").value = ""; }}</script>
</body>
</html>
Copy the code

Throw statement

The throw statement allows you to create custom errors

Technically you can throw an exception (throw an error)

Exceptions can be JavaScript strings, numbers, booleans, or objects:

throw "Too big";    // Throws text
throw 500;          // Throw a number
Copy the code

If throw is used with a try and catch, you can control the flow of the program and generate custom error messages

<! DOCTYPEhtml>
<html>
               
<body>
  <p>Please enter a number between 5 and 10:</p>
  <input id="demo" type="text">
  <button type="button" onclick="myFunction()">Detection of the input</button>
  <p id="p01"></p>
  <script>
 function myFunction() {
    var message, x;
    message = document.getElementById("p01");
    message.innerHTML = "";
    x = document.getElementById("demo").value;
    try {
                   if (x == "") throw "It's empty";
                   if (isNaN(x)) throw "Not a number.";
                   x = Number(x);
                   if (x < 5) throw "Too small";
                   if (x > 10) throw "Too much";
    } catch (err) {
                   message.innerHTML = "Input:+ err; }}</script>
</body>
</html>
Copy the code

The Error object

JavaScript has built-in error objects that provide error information when an error occurs

The error object provides two useful properties: name and message

Error object properties

attribute describe
name Sets or returns an error name
message Sets or returns an error message (a string)

Error Name Values

The name attribute of error returns six different values:

Wrong name describe
EvalError An error that has occurred in the eval() function
RangeError An error has occurred outside the numeric range
ReferenceError An invalid reference has occurred
SyntaxError A syntax error has occurred
TypeError A type error has occurred
URIError The error that has occurred in encodeURI()