In this blog, I am going to talk about types of errors in Javascript. So grab a seat, have popcorns ready.
Javascript throws runtime errors and today we are going to see how to read, understand and use those errors in your code.
In JS, an Error is an object. It has a Class Error, which has a constructor Error(). This is the generic error class in JS.
There are various types of errors that means there are various Classes of Errors.
So we can create Error objects from such constructors.
The generic constructor Error takes one argument (a message that will be used to describe the error)
So yes, you are right, if it's an object and also it has a Class, it should have properties too.
Standard Properties of Error Object:
These are 3 standard properties, there are other non-standard properties too but they might not be supported by some browsers.
Let's check below example
See the 1st word in the above error- Uncaught
: it means your error was not handled using the catch keyword.
Next word is- Error
: It is the value of the name property of the Error.
The next part is - MyMessage for the error
: It is the value of the message property in the Error.
The next part is - at <anonymous>:1:13
: This is the very important part, this is a stack trace, it shows where the error occurred, will talk about this in detail in later part of the blog.
So the above statement is just all the properties of Error showed together.
toString():
toString method when called on Error, will return a string like - name: message
If the name property value is undefined, it returns the string with name value as Error
if the message property value is undefined, it returns the string with message value as an empty string ``
We will see one example of the toString() method.
Other than the generic Error constructor, there are other core error constructors in JavaScript. We will learn some of them in this blog.
The RangeError object is thrown when a value is not in the set or range of allowed values.
Constructor: RangeError()
Properties:
ReferenceError object is thrown when a non-existing variable is referenced or used in your code.
Constructor: ReferenceError()
Properties:
The SyntaxError object is thrown when a program contains syntactically invalid code.
Constructor: SyntaxError()
Properties:
The TypeError object is thrown when an operation could not be performed, mostly when a value is not of the expected type.
Constructor: TypeError()
Properties:
URIError is thrown when a global URI method is used in a wrong way.
e.g. The decodeURI() function takes encoded URI as an argument, it throws an URIError when the encoded URI contains invalid character sequences.
Constructor: URIError()
Properties:
Let's see an example, where we handle the error using try-catch block. What if we want to handle only the TypeError and not the syntax error. We can do that easily, as we know all the errors are instances of their class. we can check their class and find out which type of error our try block has.
Let's talk about stack trace now.
consider below example. It has 3 functions, function A calls B, and function B calls C.
In function A we are handling the error, but in C error is thrown, as soon as the error is thrown in C, it stops executing further and control comes to the point where it was invoked, that means in function B. Function B also stops executing and control comes to the point where it was invoked, that means in function A. Now function A sees the catch block and the error gets caught there and now program runs further without any interruption.
Now that error tells information about type of error, message of the error and stack trace.
Stack trace information is stored in the stack property and can be helpful when trying to debug a problem. it tells us the function name where the error occurred and which functions made the failing call. States what all things are there in the stack at the time when the error occurred.
So this was all about errors in javascript. Let me know in the comments if you found this blog helpful !!
References: