Answer the question
In order to leave comments, you need to log in
How do exceptions work?
I watched videos, read articles, but nowhere is it really described how exceptions work.
Can anyone explain at a low level, step by step, how exceptions work in c++?
For more details about why the stack is untwisted there and why.
Answer the question
In order to leave comments, you need to log in
Can anyone explain at a low level, step by step, how exceptions work in c++?
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;
}
_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) <---- инициализация E::code
movl $_ZTI1E, %esi
movq %rax, %rdi
call __cxa_throw
.cfi_endproc
_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 <--- вызов функции по указателю
.LEHE0:
.LEHB1:
call _Z12s_destructorv <--- вызов деструктора объекта s при нормальном выходе из блока try
.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
.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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question