Handling errors in JavaScript

A simple example of handling errors in JavaScript with try, catch and finally.
<!DOCTYPE html>
<html>
<head>
   
</head>
<body>

<script>
"use strict";
        
    try {
        console.log("We are trying to change the value of an undeclared variable in the strict mode to 22. This will throw an error");
        result = 22;
    }
    catch(err) {
        console.log("An error has occured ---> " + err);
    }
    finally {
        console.log("The finally part will be fired regardless of the error checking result");
    }
        
</script>

</body>
</html>

Responses

0 Replies