HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>Exceptions are one of the few examples of the successful use of inheritance. In this lesson, we will learn how to create exceptions and intercept them.</p>
1 <p>Exceptions are one of the few examples of the successful use of inheritance. In this lesson, we will learn how to create exceptions and intercept them.</p>
2 <p>Usually, we use exceptions like this. Closer to the beginning of the program is the<em>try/catch</em>construct, which catches exceptions and shows the user an adequate message:</p>
2 <p>Usually, we use exceptions like this. Closer to the beginning of the program is the<em>try/catch</em>construct, which catches exceptions and shows the user an adequate message:</p>
3 <p>But how do you know what is wrong? Sometimes, it is important, because errors can lead to different program behavior. In addition, not all errors require processing at the current point in the program.</p>
3 <p>But how do you know what is wrong? Sometimes, it is important, because errors can lead to different program behavior. In addition, not all errors require processing at the current point in the program.</p>
4 <p>You can separate errors using different classes that inherit from the Error class:</p>
4 <p>You can separate errors using different classes that inherit from the Error class:</p>
5 <p>And unlike other inheritance examples, exceptions rarely need to add or change behavior. The main purpose of using exception inheritance is to describe all possible types of errors.</p>
5 <p>And unlike other inheritance examples, exceptions rarely need to add or change behavior. The main purpose of using exception inheritance is to describe all possible types of errors.</p>
6 <p>Now let's see how we can take advantage of it:</p>
6 <p>Now let's see how we can take advantage of it:</p>
7 <p>Hijacking any base exception entails automatically hijacking all descendants of the current class. For example, if you catch MyError in a<em>catch</em>block, that block will catch objects of that class and objects of all its descendants.</p>
7 <p>Hijacking any base exception entails automatically hijacking all descendants of the current class. For example, if you catch MyError in a<em>catch</em>block, that block will catch objects of that class and objects of all its descendants.</p>
8 <p>Almost every programming language has an unspoken rule about exception hierarchies. Any program must define its high-level exception, inherited from Error. All other library exceptions are inherited from it. This approach allows you to isolate the error handling of a particular library with just one<em>catch</em>block.</p>
8 <p>Almost every programming language has an unspoken rule about exception hierarchies. Any program must define its high-level exception, inherited from Error. All other library exceptions are inherited from it. This approach allows you to isolate the error handling of a particular library with just one<em>catch</em>block.</p>
9 <p>But not all libraries in JavaScript use inheritance. The point is that before the es6 standard, there were no classes and no easy way to create descendants from the Error constructor. And, since many libraries still fit in the old standard they solve the problem of determining the error type using a common property:</p>
9 <p>But not all libraries in JavaScript use inheritance. The point is that before the es6 standard, there were no classes and no easy way to create descendants from the Error constructor. And, since many libraries still fit in the old standard they solve the problem of determining the error type using a common property:</p>
10 <h2>finally block</h2>
10 <h2>finally block</h2>
11 <p>In some situations, the program may need to continue working regardless of whether an exception has occurred. If we only use<em>try/catch</em>, we can't do this without duplication. You should put code both after the whole<em>try/catch</em>construct and in every<em>catch</em>block.</p>
11 <p>In some situations, the program may need to continue working regardless of whether an exception has occurred. If we only use<em>try/catch</em>, we can't do this without duplication. You should put code both after the whole<em>try/catch</em>construct and in every<em>catch</em>block.</p>
12 <p>It has led to an expansion of the design itself, adding the<em>finally</em>block. We call this block at the very end in every case:</p>
12 <p>It has led to an expansion of the design itself, adding the<em>finally</em>block. We call this block at the very end in every case:</p>
13  
13