Try catch statement:- genius student


  • The try...catch statement is very import.
  • If any errors occur inside the try block while running time, error will throw to the catch block.
  • The finally block is optional and this will works after execute the try block or catch block.
  • The catch block contains statements which is useful when an exception is thrown from the try block.
  • Code:-

    1. <!DOCTYPE html> 
    2. <html> 
    3.  
    4. <head> 
    5.  <meta name="viewport" content="width=device-width, initial-scale=1"> 
    6. </head> 
    7.  
    8. <body> 
    9.  
    10.  <h1>Try Catch and Finally</h1> 
    11.  
    12.  <input type="button" value="Find Error" onclick="fun_err();"> 
    13.  
    14.  <script> 
    15.   function fun_err() 
    16.   { 
    17.    try 
    18.    { 
    19.     alert(i); // i is not defined so error occured 
    20.    } 
    21.    //if any error occur in try block catch will execute 
    22.    catch (err) 
    23.    { 
    24.     alert("Error is : " + err.message); 
    25.    } 
    26.    //finally is optional and it should execute after try and catch executed 
    27.    finally 
    28.    { 
    29.     alert("Finally Executed"); 
    30.  
    31.    } 
    32.   } 
    33.  </script> 
    34.  
    35. </body> 
    36.  
    37. </html>
    Try catch statement:- genius student Try catch statement:- genius student Reviewed by Network security on November 04, 2019 Rating: 5

    No comments:

    Useful Information

    Powered by Blogger.