예외처리
posted on 16 Oct 2024 under category js
💫 try…catch
<if/else문과 try/catch의 구성>
A 코드 실행
if (A에서 발생한 실수1) { try {
//A 코드 실행
} else if (A에서 발생한 실수2) { } catch (err) {
//에러 처리
} ... } ...
console.log(err.name);
console.log(err.message);
try/catch를 사용하면 코드가 종료될 때 exited with code=0
으로 되고
사용하지 않으면 exited with code=1
종료됨.
그래서 되도록 try/catch로 에러를 처리해줄 것!
throw 에러 객체
ex) throw new SyntaxError(메세지);
throw new SyntaxError("입력 값에 이름이 없습니다.");