When an exception is thrown, a C + + exception capture mechanism is immediately thrown: Figure 4 C + + exception trapping mechanism
In the previous section, we have seen the role of the nstep variable in tracking object construction and destruction. In fact, in addition to being able to track the object creation and destruction phases, Nstep is able to identify whether the current execution point is in a try block, and in which try block (if the current function has more than one try block). This is done by assigning a unique ID value to each of the entry and exit Nstep of each try block, and ensuring that the nstep changes within the corresponding try block are within this range. In implementing exception trapping, first, the C + + exception handler checks to see if the location where the exception occurred is within a try block of the current function. This work can be done by using the Nstep value of the current function in the entry in the Pihandler point to tbltryblocks[] table in the range of [Nbeginstep, Nendstep]. For example: If the FUNCB in Figure 4 has an exception in nstep = = 2 O'Clock, 2∈[1, 3 is found by comparison to the tbltryblocks[] table in the FUNCB, so the exception occurs in the first try block within the FUNCB. Second, if the exception occurs in a try block in the current function, try to match the tblcatchblocks[] table in the corresponding entry for that tbltryblocks[] . The tblcatchblocks[] table records information about all catch blocks that are associated with the specified try block, including information such as the type of exception that the catch block can catch and its start address. If a matching catch block is found, the current exception object is copied to this catch block and then jumps to its entry address to execute the in-block code. Otherwise, the occurrence of the exception is not within the try block of the current function, or there is no catch block in the try block that matches the current exception, and the above process is repeated over the Piprev address in the function stack frame (that is, the previous node in the exception handling chain). Until a matching catch block is found or the first node of the exception handling chain is reached. For the latter, we call an uncaught exception, and for C + + exception handlers, an uncaught exception is a critical error that will cause the current process to be forced to end. Note : Although the tbltryblocks[in the example in Figure 4] has only one entry, tblcatchblocks[in this entry is only a single line. However, in practice, multiple records are allowed in both tables. This means that there can be multiple try blocks in a function, and each try block can follow multiple catch blocks that are matched to it. Note : In the standard sense, the stack fallback at the time of exception is accompanied by an exception-capturing process that moves up and down the exception-handling chain. However, some compilers do a one-time stack fallback after the exception capture has been completed earlier. Regardless of the approach used, unless you are developing an embedded application in which memory is tightly constrained, it is common for us to understand the standard semantics without creating any problems. Note : There are actually some more critical fields in tblcatchblocks that are deliberately omitted. For example, indicate the field of the Catch block exception object Replication (transfer value (copy construct) or address (reference or pointer)), and where to store the copied exception object (relative to the entry address offset position) and other information. |