async function myFun(){
try{
//❗ 이 영역에서 실행한 구문이 문제가 생긴다면
}
catch(e){
//❗ 이곳에서 error를 처리한다.
// 에러가 발생하지 않는다면 이 구문은 실행하지 않는다.
}
finally{
// 항상 실행된다.
}
}
try{}
catch(){}
를 사용하면 에러가 발생하더라도 스크립트가 계속 진행된다.
catch(e){
console.log(e.name);
//👉 ReferenceError, TypeError...
console.log(e.message);
//👉 Expected property name or '}' in JSON at position 1...
}
new Error(message)
new SyntaxError(message)
new ReferenceError(message)
try{
throw new ErrorName ("내용")
}
try문 내에서 이 구문을 만나면 에러를 만난 것과 같이 catch문이 실행된다.
이때, 지정한 에러의 message가 catch(e){}
의 이벤트값 e의 .message로 반환된다.