URIError

URIError

URIError 객체는 전역 URI 처리 함수가 잘못된 방식으로 사용되었을 때의 오류를 나타냅니다. encodeURI() 또는 decodeURI() 계열 함수에 잘못된 파라미터가 전달될 때 발생합니다.

출처: URIError - JavaScript | MDN

본문

URIError 객체는 전역 URI 처리 함수가 잘못된 방식으로 사용되었을 때의 오류를 나타냅니다. 즉, 잘못된 형식(malformed)의 URI를 인자로 encodeURI(), decodeURI(), encodeURIComponent(), decodeURIComponent() 같은 함수에 전달하면 URIError가 던져집니다. URIError는 직렬화 가능한 객체이므로 structuredClone()으로 복제하거나 postMessage()로 Worker 사이에서 복사할 수 있습니다. URIErrorError의 하위 클래스입니다.

생성자

  • URIError() — 새 URIError 객체를 만듭니다.

인스턴스 속성

부모 Error의 인스턴스 속성도 상속합니다. URIError.prototype에 정의되어 모든 인스턴스가 공유하는 속성: constructor, name(오류 타입의 이름, 초기 값은 "URIError").

인스턴스 메서드

부모 Error의 인스턴스 메서드를 상속합니다.

예제

URIError 잡기decodeURIComponent("%")처럼 잘못된 URI 시퀀스를 처리할 때 URIError가 던져집니다.

try {
  decodeURIComponent("%");
} catch (e) {
  console.log(e instanceof URIError); // true
  console.log(e.message); // "malformed URI sequence"
  console.log(e.name); // "URIError"
  console.log(e.stack); // Stack of the error
}

URIError 만들기new URIError("Hello")로 직접 만들어 던질 수 있습니다.

try {
  throw new URIError("Hello");
} catch (e) {
  console.log(e instanceof URIError); // true
  console.log(e.message); // "Hello"
  console.log(e.name); // "URIError"
  console.log(e.stack); // Stack of the error
}

더 알아보기

  • Error — 모든 오류 타입의 기본 클래스
  • encodeURI(), decodeURI(), encodeURIComponent(), decodeURIComponent() — 전역 URI 처리 함수
  • TypeError, RangeError, ReferenceError, SyntaxError, EvalError — 기타 내장 오류 타입