Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Error and Exception in Javascript?

It seems that Error and Exception are the same thing: both of them can be thrown with a throw statement and caught with a try catch block.

like image 499
Jberry Avatar asked Apr 22 '13 08:04

Jberry


People also ask

What is the difference between exception and error?

The error indicates trouble that primarily occurs due to the scarcity of system resources. The exceptions are the issues that can appear at runtime and compile time. 2. It is not possible to recover from an error.

Are errors exceptions JavaScript?

Errors are thrown by the engine, and exceptions are thrown by the developer. But what exactly does that mean? In JavaScript, all exceptions are simply objects. While the majority of exceptions are implementations of the global Error class, any old object can be thrown.

What is difference between error and expectation?

Errors are usually raised by the environment in which the application is running. For example, an error will occur due to a lack of system resources. Exceptions are caused by the code of the application itself. It is not possible to recover from an error.

What is an exception in JavaScript?

An exception signifies the presence of an abnormal condition which requires special operable techniques. In programming terms, an exception is the anomalous code that breaks the normal flow of the code. Such exceptions require specialized programming constructs for its execution.


1 Answers

JavaScript syntax

Errors and exceptions are syntactically synonymous in JavaScript. The language only implements the Error keyword (through window.Error). You may define custom errors, using the Error.constructor, which takes name and message as parameters.

JavaScript Error

There is also the line number sugar that can be used to trace bug occurrences inside the code. JavaScript only has Error. Whether you hear people talking about Exceptions or Errors, in JavaScript they refer to the same thing.

Browsers make a distinction: ReferenceError (when accessing a variable with a name that doesn't exist in the heap, or when you make a typo(more here.), TypeError is also a known JS error, more here.

JavaScript Exception

A known JavaScript Exception is DOM Exception 8. It occurs when you access an object that is not ready, such as an XMLHttpRequest that has not completed the request.

Implementation

When using try catch or try catch finally blocks, you will deal with both JavaScript Exception and Error. Code-wise the difference has no impact.

Behind the scenes, the browsers use the same window.Error constructor. An Exception is an Error instance with a name and message that contain "Exception".

Try: var myCustomError = new Error("myException", "An exception occurred.");. "Exception" is text in a string. More on Error here.

Convention

By convention, there is a difference between Error and Exception. An Error indicates a clear violation. A TypeError or ReferenceError means you are not following the language specs.

An Exception is thrown when you access an XMLHttpRequest response before it is complete. Error is a "you broke the law" shout and Exception is an "Almost there!" pad on the shoulder. Hope the analogy helps!

like image 146
flavian Avatar answered Oct 05 '22 15:10

flavian