Example

Function with try & Catch

HTML

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <link name="favicon" type="image/x-icon" href="https://skillzam.com/uploads/system/e353194f3b7cd1b75d69c3ab88041985.png" rel="shortcut icon" />
    <title>Function with try & Catch</title>
</head>    
<body>
<!--
    try statement defines a code block to run (to try).
    catch statement defines a code block to handle any error.
-->

    <h1>Demo: JavaScript Functions</h1>

    <div>
        <h2>Function with try and catch</h2>
        <p id="para1"></p>
        <p id="para2"></p>
    </div>


    <!-- JavaScript code Starts here -->
    <script>

        /************************************************/
        // Function with try and catch
        // Example : Skillzam logo repeat
        //
        function logo(company) {
            try {
                document.getElementById('para2').innerHTML = 
                `${company.toUpperCase().repeat(3)}`;
            } catch (err) {
                document.getElementById('para1').innerHTML = 
                "ERROR: Please pass an argument in logo() function.";
            }
        }
        logo();             // Message "ERROR: Please pass an argument in logo() function."
        logo("skillzam.."); // SKILLZAM..SKILLZAM..SKILLZAM..

    </script>   
    <!-- JavaScript code Ends here -->

</body>
</html>

Output

View original