Maintainable JavaScript — Error Class and Checks

Maintainable JavaScript — Error Class and Checks

Creating maintainable JavaScript code is important if want to keep using the code. In this article, we’ll look at the basics of creating maintainable JavaScript code by looking at the best ways to handle errors.

Creating maintainable JavaScript code is important if want to keep using the code.

In this article, we’ll look at the basics of creating maintainable JavaScript code by looking at the best ways to handle errors.

try-catch

We should never have try-catch statements with an empty block.

They should be handled in some way so that our code won’t fail silently.

For instance, we don’t write:

try {
  errorFunc();
} catch (ex) {
  // do nothing
}

We don’t want to ignore errors.

Error Types

The ES specification specifies several kinds of built-in errors type.

Error is the base constructor for all errors.

EvalError throws when an error occurs during execution of eval .

RangeError is throw when a number is outside the bounds of its range.

They rarely occur during normal execution.

ReferenceError is thrown when an object is expected but it’s not available.

SyntaxError is thrown when code has a syntax error.

TypeError is thrown when a variable is of an unexpected type.

URIError is thrown when the URI we passed into encodeURI , encodeURIComponent , decodeURI , or decodeURIComponrnt is incorrectly formatted.

If we understand the different types of errors, then it’s easy to handle them.

All error types inherit from error.

And if we check for specific types of errors, we get better error handling.

For instance, we can write:

try {
  // ...
} catch (ex) {
  if (ex instanceof TypeError) {
    // ...
  } else if (ex instanceof ReferenceError) {
    // ...
  } else {
    // handle all others
  }
}

We check which type of error is thrown with the instanceof operator, and we can write error-handling code for each kind of error.

software-development web-development javascript technology programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

Software Developer vs Web Developer | Difference Web Developer & Software Developer

Software Developer vs Web Developer | Difference Web Developer & Software Developer | Software development and web development are normally used interchangeably. Although both include development, a software developer has different responsibilities from that of a web developer.

5 Core Criteria for Selecting Software Development Company - TopDevelopers.co

Check out these five criteria for the selection of your software vendor, and you will never regret having the wrong quality product made for you.

Offshore Software and Web Application Development Company- MobiWeb Technologies

Mobiweb Technologies is a prestigious IT company headquartered in Indore providing promising and cost effective software and web development services across the globe.

Offshore Software and Web Application Development Company- MobiWeb Technologies

Mobiweb Technologies is a prestigious IT company headquartered in Indore providing promising and cost effective software and web development services across the globe.