Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
Can return throw an exception?

While playing with C# I found that following snippet:

public int F() 
{
    try 
    {
        return 0;
    } 
    catch (Exception)
    {
        return -1;
    }
}

This generates the following asm:

Program.F()
    L0000: push ebp
    L0001: mov ebp, esp
    L0003: push esi
    L0004: sub esp, 0x14
    L0007: xor eax, eax
    L0009: mov [ebp-0x18], eax
    L000c: mov [ebp-0x14], eax
    L000f: mov [ebp-0x10], eax
    L0012: mov [ebp-0xc], eax
    L0015: xor esi, esi
    L0017: jmp short L0023
    L0019: mov esi, 0xffffffff
    L001e: call 0x6fb2d4d3
    L0023: mov eax, esi
    L0025: lea esp, [ebp-4]
    L0028: pop esi
    L0029: pop ebp
    L002a: ret

And when I remove the try and catch block:

public int F() 
{
    return 0;
}

then the generated output is:

Program.F()
    L0000: xor eax, eax
    L0002: ret

Question

As you can see JIT (Release) knows that it'll not return -1 (you can not find any branches that'll jump to return -1 case), but it does generate the try block for return 0 case. The question is can the return statement throw an exception or why does JIT generate asm for it?

Note

In contrast: this is what outputs g++(O2) for C++.

int 
f(void *this_) {
    try {
        return 0;
    }
    catch(...) {
        return -1;
    }
}

asm

f(void*):
        xor     eax, eax
        ret
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

A return statement, in general, can cause an exception due to the general purpose nature of the expression allowed in the statement — in general, an expression can cause an exception.

Should the JIT optimize exception handling knowing that the expression 0 cannot throw?  Perhaps, in a perfect world.  Is anyone complaining about this?

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda