Errors are a part of coding. Occasionally, we make mistakes as programmers. However, we are always trying to fix those mistakes by reading different resources, examining a list of error messages (also called the stacktrace), or asking for help.
Earlier in this course, we learned about two different types of errors: runtime and logic. A logic error is when your program executes without breaking, but doesn’t behave the way you thought it would. These logic errors usually require you to consider how you are going about solving the issue to resolve. Runtime errors are when your program does not run correctly, and an exception is raised.
An exception is a runtime error in which a name and message are displayed to provide more information about the error.
Exceptions and Errors
Error object. An Error Object has two properties: a name and a message.
The name refers to the type of error that occurred, while the message gives the
user information on why that exception occurred.
You have undoubtedly experienced various Exceptions already throughout this class. Let’s look at a few common Exceptions.
SyntaxError which is
console.log("This is" an example);
SyntaxError: missing ) after argument list
SyntaxError with the message:
missing ) after argument list.
ReferenceError is thrown when we try to use a variable that has not yet
ReferenceError: x is not defined
ReferenceError with the message:
x is not defined.
the provided value is a different type.
const a = "Launch"; a = "Code";
TypeError: invalid assignment to const 'a'
In this case, we declare a constant as the string “Launch”, and then try to
invalid assignment to const 'a'.
In the next section we will learn how to raise our own exceptions using the
Check Your Understanding
What is the difference between a runtime error, and a logic error?