Errors versus Exceptions - Java Script

When talking about errors in programming, you really have only two categories of primary concern: syntax errors and runtime errors.

Syntax errors,also called parsing errors, occur at compile time for traditional programming languages and at interpret time for JavaScript. These errors are a direct result of an unexpected character in the code and thus prevent it from being fully compiled/interpreted. For example, the following line causes a syntax error because it is missing a closing parenthesis:

document.write(“test”);

When a syntax error occurs, the code cannot be executed. In JavaScript,only the the same thread as the syntax error is affected. Code in other threads and other externally referenced files still execute appropriately assuming nothing in them depends on the code containing the error.

Runtime errors, also called exceptions, occur during execution (after compilation / interpretation).In this case, the problems are not with the syntax of the code.Rather, an operation attempting to complete is illegal in some way.Example:

window.openMySpecialWindow();

In the previous code,an attempt is made to access a method of the window object named openMySpecialWindow.Syntactically this line is correct, however, no such method exists. This causes the browser to return an exception.

Exceptions only affect the thread in which they occur, allowing other JavaScript threads to continue normal execution.Consider the following HTML page:

When this page loads, the handleLoad() function is called and an exception occurs when trying to call a non-existent method of the window object.That thread is then exited so the alert(“Loaded”); line is never executed. When the user clicks on the button,however,the handleClick() function is still called and an alert displays the text “Clicked.”


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Script Topics