How do the exceptions?

I watched the video, read the article, but there is not really described as a work of exception.

Can anyone explain at a low level, step by step, how exceptions work in C++?

For more detail about why there is stack unwinds and why.
June 14th 19 at 20:45
1 answer
June 14th 19 at 20:47
Can anyone explain at a low level, step by step, how exceptions work in C++?

Here is a description of the Itanium ABI associated with unwinding the stack:
https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html

ABI processors of other architectures arranged in this place just the same.

Here is a detailed description of the structures used when unwinding the stack in the code generated by gcc:
www.airs.com/blog/archives/460
www.airs.com/blog/archives/464

Documents linked quite difficult to understand. For ease of understanding, you can compile a simple C++ code throwing and catching an exception and find the parts described in the first document.
For example
void e_destructor();
void s_destructor();

struct E {
 int code;

 E(int c): code(c)
{
}
~E()
{
e_destructor();
}
};

struct S {
~S()
{
s_destructor();
}
};

void f(int v)
{
 throw E(v);
}

int g(void (*p)(int v) int v)
{
 try {
 struct S S;
p(v);
 } catch(struct E E) {
 return e.code;
 } catch (int i) {
 return i;
 } catch (...) {
throw;
}
 return 0;
}

after g++ -O2 -S turns into the following segments:
a function f:
_Z1fi:
.LFB9:
.cfi_startproc
 pushq %rbx
 .cfi_def_cfa_offset 16
 .cfi_offset 3, -16
 movl %edi, %ebx
 movl $4, %edi
 call __cxa_allocate_exception
 movl $_ZN1ED1Ev, %edx
 movl %ebx, (%rax) <---- initializing E::code
 movl $_ZTI1E, %esi
 movq %rax, %rdi
 call __cxa_throw
 .cfi_endproc

Here you can see the calls to __cxa_allocate_exception constructor of the object class E and __cxa_throw
the function g:
_Z1gPFviEi:
.LFB10:
.cfi_startproc
 .cfi_personality 0x3,__gxx_personality_v0
 .cfi_lsda 0x3,.LLSDA10
 pushq %rbp
 .cfi_def_cfa_offset 16
 .cfi_offset 6, -16
 pushq %rbx
 .cfi_def_cfa_offset 24
 .cfi_offset 3, -24
 movq %rdi, %rax
 movl %esi, %edi
 subq $8, %rsp
 .cfi_def_cfa_offset 32
.LEHB0:
 call *%rax <--- calling function by pointer
.LEHE0:
.LEHB1:
 call _Z12s_destructorv <--- call the destructor of the object s when the normal exit from a try block
.LEHE1:
 xorl %eax, %eax
.L17:
 addq $8, %rsp
.cfi_remember_state
 .cfi_def_cfa_offset 24
 popq %rbx
 .cfi_def_cfa_offset 16
 popq %rbp
 .cfi_def_cfa_offset 8
 ret

Tail with exception handlers:
.L13:
.cfi_restore_state
 movq %rdx, %rbx
 movq %rax, %rbp
 call _Z12s_destructorv
 movq %rbx, %rdx
.L6:
 cmpq $1, %rdx
 je .L8
 cmpq $2, %rdx
 jne .L22
 movq %rbp, %rdi
 call __cxa_begin_catch
 movl (%rax), %ebx
 call __cxa_end_catch
 movl %ebx, %eax
 jmp .L17
.L14:
 movq %rax, %rbp
 jmp .L6
.L22:
 movq %rbp, %rdi
 call __cxa_begin_catch
.LEHB2:
 call __cxa_rethrow
.LEHE2:
.L8:
 movq %rbp, %rdi
 call __cxa_get_exception_ptr
 movq %rbp, %rdi
 movl (%rax), %ebx
 call __cxa_begin_catch
.LEHB3:
 call _Z12e_destructorv
.LEHE3:
.LEHB4:
 call __cxa_end_catch
.LEHE4:
 movl %ebx, %eax
 jmp .L17
.L16:
 movq %rax, %rbx
 call __cxa_end_catch
 movq %rbx, %rdi
.LEHB5:
 call _Unwind_Resume
.LEHE5:
.L15:
 movq %rax, %rbx
 call __cxa_end_catch
 movq %rbx, %rdi
.LEHB6:
 call _Unwind_Resume
.LEHE6:
 .cfi_endproc

Here you can see the calls to __cxa_begin_catch and __cxa_end_catch, __cxa_rethrow re-throwing the caught exception, __cxa_get_exception_ptr and _Unwind_Resume called if the catch block does not catch this exception.

Next comes the structure of the LSDA is described in the third document.

She unwinding the stack in this code is missing. It runs the following code libgcc: phase 1 and phase 2.

Find more questions by tags C++Exceptions