예외처리 throw, try, catch

var add = function(a, b){
  if(typeof a !== 'number' || typeof b !== 'number'){
    throw{
      name: 'TypeError',
      message: 'add needs numbers'
    }
  }
  return a + b;
};

var try_it = function(){
  try{
    add("seven");
  } catch(e){
    console.log(e.name + ": " + e.message);
  }
}
try_it();

throw 에서 예외발생시 표시할 속성들을 만들어 놓는다.
try 블록 안에서 예외가 발생하면 catch 로 던져진다.
catch 블록 안에서 throw 객체의 속성들을 이용해서 처리할 수 있다.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
모든 댓글 보기
0
생각을 나눠주시면 감사해요!x
()
x
Please enter Google Username or ID to start!
Example: clip360net or 116819034451508671546
Title
Caption
File name
Size
Alignment
Link to
  Open new windows
  Rel nofollow