JavaScript Errors

JavaScript Errors


JavaScript Errors
In this tutorial, you will learn about JavaScript Errors: There are three types of errors in JavaScript are as follows:-
  • Syntax Errors,
  • Runtime Errors and 
  • Logical Errors
Syntax Errors
An exception caused by the incorrect use of a predefined syntax. Syntax errors are detected while compiling or parsing source code. 
For Example, the following line causes a syntax error because it is missing a closing parenthesis:-
Example
<script type = “ text/javascript”>
     <! - -
          Window.print(;
     //-->
</script>
Runtime Errors 
Runtime errors occur during the run-time of the program. It is also known as the exception. In the example that is given below the syntax is correct, but at runtime, it is trying to call a method that does not exist.
Syntax:
<Script type = “text/javascript”>
   <! - - 
         try 
         {
            // Here the main Code run 
            [ break; ]
         }
        Catch ( exception e )
         {
           // The code will run when there is an exception
          }
// - - >
</script>
 
Example - 
Output - 
Logical Errors -
Logical Errors are the most difficult to debug. They occur when you make logical mistakes, for example business mistakes or conceptual mistakes. JavaScript cannot handle these errors by itself because it completely depends on your business which JavaScript is not aware of. You may use throw  to let you know about the error.
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 try, catch, throw and finally Statements. Whenever an error occurs the technical term we use for it is throwing an exception.
Example -
Output -