JavaScript/이론
[Javascript] 예외처리
sirius
2021. 3. 16. 10:43
try : 실행할 코드 입력
catch : try 블록 내에서 예외가 발생할 경우 호출되는 블록
finally : try 블록 안에서 예외가 발생하거나, 발생하지 않거나 상관 없이 무조건 실행되는 블록
throw : 예외를 강제로 발생시켜야 할 경우
function customException(name, message) {
this.name = name;
this.message = message;
}
function exceptionTest(param) {
try {
console.log('try : ' + param);
if( param == 'custom') {
throw new customException('customException', 'customException 강제발생');
}
} catch(e) {
if(e instanceof customException) {
console.log(e.name + "::" + e.message);
}
console.log('catch : ' + e);
} finally {
console.log("finally");
}
}
exception 함수를 정의할 수 있으며, instanceof를 이용하여 분기처리도 가능