Reading-Notes

error

When executing JavaScript code, errors will most definitely occur. These errors can occur due to the fault from the programmer’s side or the input is wrong or even if there is a problem with the logic of the program. But all errors can be solved and to do so we use five statements that will now be explained.

error

The try statement lets you test a block of code to check for errors.

The catch statement lets you handle the error if any are present.

The throw statement lets you make your own errors.

The finally statement lets you execute code, after try and catch. The finally block runs regardless of the result of the try-catch block.

Error Handling | Debugging

Dealing with errors actually involves two very different processes: error handling and debugging. Error handling is a combination of coding and methodology that allows your program to anticipate user and other errors.

It allows you to create a robust program. Error handling does not involve weeding out bugs and glitches in your source code, although some of the error handling techniques covered in this chapter can be used to great advantage at the debugging stage.

In general, error handling should be part of your overall program plan, so that when you have an error-free script, nothing is going to bring it to a screeching halt.

With some sturdy error handling in place, your program should be able to keep running despite all the misuse that your users can—and certainly will—throw at it.