Error Handling tutorial in Advance JavaScript

 <!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Error Handling Advance JavaScript</title>
    <link rel="stylesheet" href="">
</head>

<body>
    <script>
        // try-catch works Synchronously, try-catch is used for handle Error
        // Example 1 :
        // try {
        //     console.log("Start of Try");
        //     blabla();
        //     console.log("End of Try");
        // }
        // catch(error) {
        //     // console.log("Error has occurred.");
        //     console.log(error);
        // }

        // Example 2 : Below Code gives not correct output
        // try {
        //     setTimeout(function () {
        //         blabla;
        //     }, 1000);
        // }
        // catch (error) {
        //     console.log("Error has occurred.");
        // }

        // Example 3 : Proper usage of try-catch block with setTimeout() is as shown below :
        // setTimeout(function () {
        //     try {
        //         blabla;
        //     }
        //     catch (error) {
        //         console.log("Error has occurred.");
        //     }
        // }, 1000);

        // Example 4 :
        // try {
        //     blabla;
        // }
        // catch(error) {
        //     console.log(error.name);
        //     console.log(error.message);
        //     console.log(error.stack);
        // }

        // Example 5 :
        // try {
        //     blabla;
        // }
        // catch(error) {
        //     if(error instanceof ReferenceError) {
        //         console.log("ReferenceError");
        //     }
        //     else if(error instanceof TypeError) {
        //         console.log("TypeError");
        //     }
        //     else {
        //         console.log("Unknown Error");
        //     }
        // }

        // Example 6 :
        try {
            let json = `{
                "name": "Hello World",
                "age": 30
            }`;
            // JSON.parse() method converts JSON to JavaScript Object
            // JSON.stringify() method converts JavaScript Object to JSON
            let user = JSON.parse(json);

            if (!user.name) {
                throw new Error("Incomplete data : No name");
            }

            console.log(user.name);
            console.log(user.age);
        }
        catch (error) {
            console.log(error);
        }
        //  If "try" has Error OR "try" has not any Error but "finally" will always Execute as shown below :
        finally {
            console.log("Finally is Called.");
        }
    </script>
</body>

</html>



Comments

Popular posts from this blog

Generators tutorial in Advance JavaScript

Document Object Module DOM querySelector and querySelectorAll tutorial in JavaScript

Find Even and Odd Numbers with Loops tutorial in JavaScript