JavaScript/Basic

Error 종류

yunajoe 2022. 12. 29. 19:39

에러의 3가지 스타일 
1. 존재하지 않는 변수나 함수를 호출했을 때 
Uncaught ReFerenceEror 

// 에러와 에러 객체 

console.log('시작'); 
const coffee ='아메리카노'; 

console.log(coffee); 
console.log(size); 


console.log('끝')
index.js:7 Uncaught ReferenceError: size is not defined




2. 함수가 아닌 것을 함수처럼 사용하였을 떄 
Uncaught TypeError:

// 에러와 에러 객체 

console.log('시작'); 
const coffee = '아메리카노'; 

console.log(coffee()); 
console.log(size); 


console.log('끝')
index.js:6 Uncaught TypeError: coffee is not a function




3. 문법에 맞지 않는 코드를 작성하였을 때 
Uncaught SyntaxError

// 에러와 에러 객체 

console.log('시작');  

const coffee-123 = '아메리카노';  
console.log(coffee()); 
console.log(size); 


console.log('끝')
Uncaught SyntaxError: Missing initializer in const declaration

 

 

에러 객체 만들기 & 던져주기 

 

// 에러와 에러 객체 
console.log('시작'); 

// 에러 객체 만들기 
const error = new TypeError('타입에러가발생하였습니다');  

throw error; // 에러 발생시키기  
console.log(error.name); 
console.log(error.message);

console.log('끝');