Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
¿Cómo regresar si ocurre un error dentro de un bloque `try-catch` y aún así ejecutar el código finalmente?

Estoy tratando de salir de la función si ocurre un error dentro del bloque de try , y sigo ejecutando el código de limpieza finally .

Lo estoy haciendo usando shouldContinue que está establecido en true inicialmente, y lo configuro en false dentro del bloque catch si la ejecución no debe continuar.

 async uploadToServer() { let response; let shouldContinue = true; try { response = await this.uploderService.uploadFileToServer(); } catch (error) { this.displayUploadError(error); shouldContinue = false; } finally { // run cleanup code anyway this.resetSession(); } if (!shouldContinue) { return; } this.saveResponse(response); // continue execution here // ... }

¿Hay una mejor manera de salir de la función después de que ocurra un error dentro del bloque de try y aún así ejecutar el código finally ?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Una forma (probablemente mi forma preferida personal) es simplemente poner todo dentro del intento (si es posible, por ejemplo, si nada más dentro del intento podría arrojar un error no relacionado):

 async uploadToServer() { try { const response = await this.uploderService.uploadFileToServer(); this.saveResponse(response); // continue execution here // ... } catch (error) { this.displayUploadError(error); } finally { // run cleanup code anyway this.resetSession(); } }

Otra forma es regresar en la captura (finalmente todavía se ejecuta):

 async uploadToServer() { let response; try { response = await this.uploderService.uploadFileToServer(); } catch (error) { this.displayUploadError(error); return; } finally { // run cleanup code anyway this.resetSession(); } this.saveResponse(response); // continue execution here // ... }
about 4 years ago · Juan Pablo Isaza Report

0

Finalmente se ejecutará de todos modos, ya sea el intento ejecutado o la captura. Consulte la descripción en el siguiente enlace para obtener más información.
Referencia del desarrollador de Mozilla

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!