프로그래밍/JavaScript
javascript - throw 예외처리
아빠개발자
2022. 1. 5. 13:51
반응형
📢JAVASCRIPT THROW 예외처리
▼ 내용
throw 에 에러가 걸리면 이하 구문은 실행안하고 catch문으로 들어간다
💻코드
function getRectArea(width, height) {
if (isNaN(width) || isNaN(height)) {
console.log("시작 1");
throw "Parameter is not a number!";
console.log("시작 2");
}
console.log("시작 3");
}
console.log("시작 4");
try {
getRectArea(3, 'a');
console.log("시작 5");
}
catch(e) {
console.log("에러22"+e);
// expected output: "Parameter is not a number!"
}
//console.log 에러일경우
> "시작 4"
> "시작 1"
> "에러22Parameter is not a number!"
//console.log 정상일경우
> "시작 4"
> "시작 3"
> "시작 5"
반응형