📌 예외 객체

예외객체란 (exception object)

예외가 발생하면 예외와 발생한 정보를 확인할 수 있게 해주는 것.

try catch 구문을 사용할 때 catch의 인자로 입력하는 식별자

try {

} catch (**exception**) {   // exception = 예외 객체
 
} 

그러나 자바스크립트는 다른 언어와 비교해서 예외가 거의 발생하지 않는 언어인데,

이는 프로그램에 수많은 버그를 일으킬 수 있는 요소가 되기도 한다.

⇒ throw 키워드를 사용하여 예외 강제 발생시킴.

예외 객체 출력 결과

try {
  const array = new Array(999999999999999);
} catch (exception) {
  console.log(exception);

  console.log();

  console.log(`예외 이름: ${exception.name}`);
  console.log(`예외 메시지: ${exception.message}`);
}
RangeError: Invalid array length
    at Object.<anonymous> **(/Users/songminhyeong/Prac_Project/source/06/test.js:2:17)**
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

예외 이름: RangeError
예외 메시지: Invalid array length

e.name

에러 이름. "RangeError"