Skip to content

오류 처리 ​

던져진 오류는 좋은 현상입니다! 이는 런타임이 프로그램 내 문제 발생 지점을 정확히 식별했으며, 현재 스택에서 함수 실행을 중단하고(Node 환경에서는 프로세스를 종료)콘솔에 스택 트레이스를 출력함으로써 이를 개발자에게 알린다는 의미입니다.

포착된 오류를 무시하지 마세요 ​

예외 처리된 오류에 아무 조치도 취하지 않으면 해당 오류를 수정하거나 대응할 기회를 상실하게 됩니다. 콘솔 로깅은 무시하는 것에 비해 크게 개선되지 않으며 종종 다른 출력물에 묻히곤 합니다. try/catch로 코드를 감쌌다는 것은 해당 영역에서 오류 발생 가능성을 예상한 것이므로 반드시 처리 계획을 수립하거나 코드 경로를 마련해야 합니다.

나쁜 사례:

javascript
try {
  functionThatMightThrow();
} catch (error) {
  console.log(error);
}

좋은 사례:

javascript
try {
  functionThatMightThrow();
} catch (error) {
  // One option (more noisy than console.log):
  console.error(error);
  // Another option:
  notifyUserOfError(error);
  // Another option:
  reportErrorToService(error);
  // OR do all three!
}

거부된 프로미스를 무시하지 마세요 ​

try/catch에서 포착된 오류를 무시하지 말아야 하는 것과 동일한 이유가 적용됩니다.

나쁜 사례:

javascript
getdata()
  .then(data => {
    functionThatMightThrow(data);
  })
  .catch(error => {
    console.log(error);
  });

좋은 사례:

javascript
getdata()
  .then(data => {
    functionThatMightThrow(data);
  })
  .catch(error => {
    // One option (more noisy than console.log):
    console.error(error);
    // Another option:
    notifyUserOfError(error);
    // Another option:
    reportErrorToService(error);
    // OR do all three!
  });