Tratamento de Erros

  • try catch finally

try {
    const json = '{ nome: "Bruno", "dataNascimento": "09061993" }'
    const user = JSON.parse(json)
  
    console.log(user.nome)
  } catch (e) {
    console.log(e.message)     // Unexpected token n in JSON at position 2

  }
  finally{
    console.log('finally');  // Também será executado
  }
  • throw

  if (typeof valor !== 'number') {
    throw 'O valor precisa ser um número';
  }
  • Error

new Error(message)
throw new Error('New error message', { cause: err });

Last updated