JavaScript

1. Java Script Module 2. World Wide Web (WWW) 3. Web Terms 4. NEED FOR SCRIPT 5. Introduction to Internet Programming 6. CLIENT SIDE and SERVER SIDE Scripting 7. Different Scripting Languages 8. Introduction to JavaScript 9. JavaScript and Security 10. How to Enable JavaScript 11. Advantages of Java Script 12. Limitations with JavaScript 13. Difference between JavaScript and JAVA 14. Difference between JavaScript and VBScript 15. JavaScript Development Tools 16. JavaScript Programming Techniques 17. JavaScript terms 18. Starting with JavaScript Programming step by step 19. Time to start JavaScript Programming 20. JavaScript Variables 21. JavaScript RESERVED WORDS 22. JavaScript Data Type 23. NUMBER DATA TYPE 24. BOOLEAN DATA TYPE 25. STRING DATA TYPE 26. NULL DATA TYPE 27. JavaScript Undefined DATA TYPE 28. JavaScript Operators 29. JavaScript Arithmetic Operators 30. JavaScript Logical Operators 31. JavaScript Comparison Operators 32. JavaScript String Operators 33. Bit Manipulation Operators 34. JavaScript Assignment Operators 35. JavaScript Conditional Operator 36. Conversion between JavaScript Data Types 37. Alert JavaScript function 38. confirm JavaScript function 39. prompt JavaScript function 40. JavaScript Statements 41. Single line & Multi-line Comments 42. JavaScrip If Statement 43. JavaScript if..else Statements 44. JavaScript IfÖelse..if statement. 45. JavaScript For Statement 46. JavaScript For..in Statement: 47. JavaScript While Statement 48. JavaScript do while Statement: 49. JavaScript break and continue statement 50. JavaScript switch Statement: 51. JavaScript function() 52. JavaScript function with a Return Value 53. JavaScript with Statement 54. try, catch, and throw Statements 55. JavaScript ARRAY 56. JavaScript array of different data types 57. JavaScript ARRAY and Bubble sorting Program 58. JavaScript Array Methods 59. concate() method 60. every() method 61. filter() method 62. forEach() method 63. indexOf() method 64. Join() method 65. lastIndexOf() method 66. map() method 67. reduce() method 68. reduceRight() method 69. reverse() method 70. some() method 71. toSource() method 72. toString() method 73. pop() method 74. push() method 75. shift () method 76. slice () method 77. splice () method 78. sort() method 79. unshift() method 80. EVENT handling with JavaScript 81. onblur JavaScript Event 82. onchange JavaScript Event 83. onclick JavaScript Event 84. onDblClick JavaScript Event 85. onfocus JavaScript Event 86. onkeydown JavaScript Event 87. onkeyup JavaScript Event 88. Onkeypress JavaScript Event 89. onLoad JavaScript Event 90. onMousedown JavaScript Event 91. onMouseup JavaScript Event 92. Onmouseover JavaScript Event 93. onmouseout JavaScript Event 94. onMouseMove JavaScript Event 95. onReset JavaScript Event 96. onsubmit JavaScript Event 97. onUnload JavaScript Event 98. The Screen Object 99. JavaScript Interview Part1 100. JavaScript Interview Part2 101. JavaScript Interview Part3 102. JavaScript Interview Part4 103. JavaScript Interview Part5 104. JavaScript Interview Part6 105. JavaScript Interview Part7 106. JavaScript Interview Part8 107. JavaScript Interview Part9 108. JavaScript Interview Part10
Pr.Pg Next Pg

try, catch, and throw Statements tutorials

  • The try statement lets you test a block of JavaScript code for errors.

  • The catch statement lets you handle a JavaScript error.

  • The throw statement lets you create custom errors.

  • These statements work together to give you advanced control over any errors and exceptions that may arise in your code.

  • The try statement and the catch statement work together to support exception handling.

 

Syntax:

try

{

statements(s) where exception may be thrown

}

catch (err)

{

statement(s) that handle the exception

}

  • The try statement surrounds the statements for which exception handling is to be performed. It is immediately followed by a catch statement that performs the exception handling. The errorVariable is used to reference any exception that occurs. It is assigned an instance of the error object. If an exception is thrown during the processing of the statements contained within the try statement, then errorVariable is assigned an error object that identifies the exception, and control immediately is transfers to the statements contained within the catch statement. If no exception is thrown during the processing of the statements contained within the try statement, then the catch statement is skipped and control transfers to the statement following the catch statement.

  • The throw statement is used to throw an exception.

throw expression

the value of the expression is used to the identify the type of error that occurred. For example, the following statement throws an exception named BadInputFromUser.

throw "BadInputFromUser"

 

Example: Program to demonstrate JavaScript try and catch statement
Output:
Example: JavaScript Example an exception handling using the try & catch statement.
Output:

Testing 0: It's out of range.

Testing 1: It's prime.

Testing 2: It's prime.

Testing 3: It's prime.

Testing 4: It's divisible by2.

Testing 5: It's prime.

Testing 6: It's divisible by2.

Testing 7: It's prime.

Testing 8: It's divisible by2.

Testing 9: It's divisible by3.

Testing 10: It's divisible by2.

Testing 11: It's out of range.

 

  • From the above example, the for statement iterates I between 2 and n. if n modulus I is zero, then n is not prime, the itís divisible by exception is thrown, and control passes to the catch statement. Otherwise, Itís prime is written to the document window and the function returns.

  • The catch statement simply writes the exception to the document window. The output that is displayed by primeTest() as the result of the scriptís processing.

  • The writeln() method is identical to the write() method, with the addition of writing a newline character after each statement.

 

Example: JavaScript example of an exception handling using the try catch and throw statement.
Output:

 

  • From the above example, determines the value of a variable called x. If the value of x is higher than 10 or lower than 0 we are going to throw an error. The error is then caught by the catch argument and the proper error message is displayed.

 

Pr.Pg border                                              Next Pg