SyntaxError
객체는 문법적으로 유효하지 않은 코드를 해석하려고 시도할 때 발생하는 오류를 표현합니다.
설명
SyntaxError
는 JavaScript 엔진이 코드를 분석할 때 문법을 준수하지 않은 코드를 만나면 발생합니다.
구문
new SyntaxError([message[, fileName[, lineNumber]]])
매개변수
message
Optional- 오류에 대한 설명.
fileName
Optional- 예외가 발생한 코드를 담고 있는 파일의 이름.
lineNumber
Optional- 예외가 발생한 코드의 라인 넘버.
속성
SyntaxError.prototype
SyntaxError
객체에 속성을 추가할 수 있도록 해주고 있습니다.
메서드
전역(global) SyntaxError
는 고유의 메소드를 가지고 있지 않지만, prototype chain을 통해 몇몇의 메소드를 상속 받습니다.
SyntaxError
인스턴스
속성
메소드
예제
SyntaxError
잡기
try {
eval('hoo bar');
} catch (e) {
console.log(e instanceof SyntaxError); // true
console.log(e.message); // "missing ; before statement"
console.log(e.name); // "SyntaxError"
console.log(e.fileName); // "Scratchpad/1"
console.log(e.lineNumber); // 1
console.log(e.columnNumber); // 4
console.log(e.stack); // "@Scratchpad/1:2:3\n"
}
SyntaxError
생성하기
try {
throw new SyntaxError('Hello', 'someFile.js', 10);
} catch (e) {
console.log(e instanceof SyntaxError); // true
console.log(e.message); // "Hello"
console.log(e.name); // "SyntaxError"
console.log(e.fileName); // "someFile.js"
console.log(e.lineNumber); // 10
console.log(e.columnNumber); // 0
console.log(e.stack); // "@Scratchpad/2:11:9\n"
}
명세
Specification | Status | Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Standard | 초기 정의. |
ECMAScript 5.1 (ECMA-262) The definition of 'SyntaxError' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'SyntaxError' in that specification. |
Standard | |
ECMAScript (ECMA-262) The definition of 'SyntaxError' in that specification. |
Living Standard |
브라우저 호환성
BCD tables only load in the browser