Introduction to exceptions in JavaScript

Now we will look at exceptional situations in JavaScript. To begin with, I will make some introduction, in which there will be examples of exceptional situations in general for any programming language, and then we will figure out how things are in JavaScript.

When we write our programs, we implicitly rely on the fact that all the software and hardware mechanisms we use will work correctly.

This, however, is not always the case. When data is transmitted over the network, the connection is interrupted and the data comes to us in an incorrect form, or doesn't come at all. When writing a file, it turns out that the space allocated to us on the hard disk has run out, and the file cannot be written. When reading a file, it turns out that such a file doesn't exist and we have nowhere to read from. When printing data on the printer, the cable between the printer and the computer breaks.

All the described situations have a common essence: a certain failure occurs, which leads to the impossibility or senselessness of completing the planned operation.

There are also situations in which some kind of error occurs that is not a failure. For example, you ask the user for his email, and he enters the email in an incorrect format. It is clear that our program cannot continue to process email, as it is not correct. However, this is not an exceptional situation. Our program can correct the situation itself: it will display an error message and the user will repeat his input.

In fact, the difference between failing and not failing is quite vague. An event that one program might treat as an exception may be treated by another program as some kind of error that it can handle.

The criterion here is the following: if, when a problem occurs, your program can continue to do what it is intended for, then this is not an exception, and if it cannot, then yes, this is an exception.

For example, we have a program that needs to ask the user's email address. If the user entered an email in the wrong format, this is not a failure. This is an expected problem and our program will ask the user for an email many times until he enters it correctly.

Suppose our program that asks for email should also send that valid email over the Internet. It turns out that the Internet is not working. This is already a problem: the program will not be able to send data over the Internet if the Internet is down. The program, however, can continue its execution: it can display information about the problem, retry sending after a while, and so on. But these actions are not quite what the program was intended for, since the program cannot do the main action - sending an email.

Very often, whether an exception occurs or not depends on the programming language. In many languages, if suddenly a division by zero occurs, this is considered an exception (since you cannot divide by zero), but in JavaScript it is not considered (in JavaScript, you can divide by zero).

enru