JS 고급-(8) 에러 핸들링

김수민·2022년 12월 1일
0

JavaScript

목록 보기
20/27

try...catch

async function myFun(){
      try{
		//❗ 이 영역에서 실행한 구문이 문제가 생긴다면
      }
  	  catch(e){
        //❗ 이곳에서 error를 처리한다.
        // 에러가 발생하지 않는다면 이 구문은 실행하지 않는다.
      }
      finally{
      	// 항상 실행된다.
      }
    }

try{}
catch(){}
를 사용하면 에러가 발생하더라도 스크립트가 계속 진행된다.

error.

catch(e){
  console.log(e.name);
  //👉 ReferenceError, TypeError... 
  console.log(e.message);
  //👉 Expected property name or '}' in JSON at position 1...
}

Error

new Error(message)

SyntaxError

new SyntaxError(message)

ReferenceError

new ReferenceError(message)

throw new

try{
	throw new ErrorName ("내용")
}

try문 내에서 이 구문을 만나면 에러를 만난 것과 같이 catch문이 실행된다.
이때, 지정한 에러의 message가 catch(e){}의 이벤트값 e의 .message로 반환된다.

profile
sumin0gig

0개의 댓글