Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
Is there a way to stub/intercept a javascript statement 'throw' in vanilla javascript?

We can stub any function or class like this,

class ErrorStub{
        constructor(message){
                // do whatever with 'message'
        }
}
Error = ErrorStub
new Error('This will go to ErrorStub now')

Similar to this, is there any way we can intercept a 'throw' statement. So that whatever exception is thrown across whole website, can be handled in one place?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Wouldn't this satisfy your needs? You would need to listen to the error event, handle it the way you want and return false

function interceptError(exception) {
  // Here you can write code which intercepts before
  // the error gets triggered and printed in the console
  alert(`Exception occured: ${exception.message}`)
}

window.addEventListener("error", function (e) {
  interceptError(e);
  return false;
})

throw new Error('Test exception');

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda