JavaScript Debugging

JavaScript Debugging


JavaScript Debugging
In this tutorial, you will learn about JavaScript Debugging is as follows:
Three types of errors can occur in a program:
  • Syntax errors
  • Runtime errors
  • Logical errors
Syntax errors occur when you enter code that the interpreter does not recognize.
Syntax Errors in JavaScript include invalid statements that are entered incorrectly.
Example
<script type = “ text/javascript”>
     <! - -
          Window.print(;
     //-->
</script>
 
Runtime errors, if the JavaScript interpreter encountered a problem while a program is executing, that problem is called a run-time error.
Logical errors are problems in the design of a program that prevent it from being anticipated.
Error messages
The first line of defense in locating bugs in JavaScript programs are the error messages you receive when the JavaScript interpreter encounters syntax or run-time error.
Two important pieces of information displayed in error message dialog boxes by both Navigator and Internet Explorer are:
  • The line number in the document where the error occurred.
  • A description of the error
 Syntax:
 
 
Example - 
https://lh4.googleusercontent.com/cjJFqIEC3TBa6e1eBxNR29IgJ7Zr6UK_akJo74a3Y08nguI1wR7RfcHkkMZlpFBncU48dK_Ddwpw308S38-lhVG5YrKKxXycZh94UBd5BdxkQ63poYXjvXjGBv-pgbBLiFni9j8h
Output - 
https://lh3.googleusercontent.com/6s8NQ4d7JFPz36o1lH4cP-L4nTkWMsUnzKYS83WaJ9JM0Q2TPMDcexq39xE0-HG15geArP_Bqrd75XA7mJBEz8tZwIww2EvfIRnITRlHBpcmrPJ_pVEtc5aUyT5hwHfzDjCHn7u3
Handling Errors 
Now, as we know about programming error types, let’s see how we can handle these errors in JavaScript. For the purpose we use trycatchthrow and finally Statements. Whenever an error occurs the technical term we use for it is throwing an exception.
The Syntax of the try…catch…finally Statement in JavaScript is as follows:
try {
// block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
// ...
finally {
// block of code to be executed after try block ends
}
Example 1 -
Output -
Example 2 -
 
Output -