Apply for Zend Framework Certification Training

Javascript





Exception handling 

An error is an action that is inaccurate or incorrect. There are three types of errors in programming which are discussed below:

Syntax error   Logical error Runtime error

Syntax error: According to computer science, a syntax error is an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular programming language or it is also a compile-time error if the syntax is not correct then it will give an error message. 

As the syntax is not correct of the JavaScript it will affect only the thread that is under this JavaScript and the rest of the code in other threads gets executed as nothing in them depends on the code containing the error. 

Logical error: It is the most difficult error to be traced as it is the error on the logical part of the coding or logical error is a bug in a program that causes it to operate incorrectly and terminate abnormally (or crash). 

Runtime Error: A runtime error is an error that occurs during the running of the program, 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. 

try-catch method

JavaScript uses the try catch and finally to handle the exception and it also uses the throw operator to handle the exception. try have the main code to run and in the catch block if any error is thrown by try block will be caught and the catch block will execute. Finally block will always occur even if an error is thrown

The try statement lets you test a block of code to check for errors.

The catch statement lets you handle the error if any are present.

The throw statement lets you make your own errors.

The finally statement lets you execute code after try and catch.  

The finally block runs regardless of the result of the try-catch block.

try {

      let a = 10;

      if (a > 10) {

            console.log(a);

      } else {

        throw new Error("This is an custimized error");

      }

} catch (error) {

      console.log(error.message);

} finally {

      console.log("finally");

}

 

< Array in javascript Dynamically create textbox using javascript >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top