Reason
  • 문서
  • 해보기
  • API
  • 커뮤니티
  • 블로그
  • Languages icon한국어
    • 日本語
    • English
    • Deutsch
    • Español
    • Français
    • Português (Brasil)
    • Русский
    • Українська
    • 中文
    • 繁體中文
    • 번역 돕기
  • GitHub

›Advanced Features

소개

  • What & Why

Setup

  • 설치
  • 에디터 플러그인

언어 기본

  • 개요
  • Let 바인딩
  • 원시 타입
  • 기본 자료구조
  • 타입
  • 레코드
  • Variant
  • Options and nullability
  • 함수
  • 재귀
  • 비구조화
  • 패턴 매칭
  • Mutable Bindings
  • 반복문
  • Modules

Advanced Features

  • JSX
  • 외부 접근
  • 예외
  • 오브젝트

JavaScript

  • 연동
  • 문법 치트시트
  • Pipe First
  • 프라미스
  • 라이브러리
  • JS에서 변환

추가 사항

  • 자주 물어보는 질문
  • 추가적으로 매력적인 것들
Translate

예외

예외는 특별한 유형의 variant입니다, 예외적인 경우에 "던져집니다" (남용하지 마세요!).

사용

let getItem = (theList) =>
  if (callSomeFunctionThatThrows()) {
    /* 찾은 아이템을 반환 */
  } else {
    raise(Not_found)
  };

let result =
  try (getItem([1, 2, 3])) {
  | Not_found => 0 /* getItem이 에러를 던질 시의 기본 값 */
  };

Note that the above is just for demonstration purposes; in reality, you'd return an option(int) directly from getItem and avoid the try altogether.

You can directly match on exceptions while getting another return value from a function:

switch (List.find((i) => i === theItem, myItems)) {
| item => print_endline(item)
| exception Not_found => print_endline("아이템을 찾을 수 없습니다!")
};

You can also make your own exceptions like you'd make a variant (exceptions need to be capitalized too).

exception InputClosed(string);
...
raise(InputClosed("스트림이 닫혀있습니다!"));

팁 & 트릭

보통의 경우, 예외는 필요 없을 겁니다. 예를 들어, 콜렉션에서 item을 찾을 수 없을 때 예외를 던지는 대신, option(item)을 반환(찾을 수 없는 경우에는 None을 반환)하는 거죠.

디자인 결정

The above tip seems to contradict what's happening in the OCaml standard library; prominent functions in modules such as List and String seems to throw exceptions overly often. This is partially a historical sediment, and partially out of extreme care for performance. Native OCaml/Reason is incredibly performant; exception throwing was designed to be very cheap, cheaper than allocating and returning e.g. an option. This is unfortunately not the case for JavaScript.

Newer standard library alternatives usually come with option-returning functions rather than exception-throwing ones. For example, List.find has the option-returning counterpart List.find_opt, which doesn't throw.

Exceptions are actually just variants too. In fact, they all belong to a single variant type, called exn. It's an extensible variant, meaning you can add new constructors to it, such as InputClosed above. exception Foo is just a sugar for adding a constructor to exn.

← 외부 접근오브젝트 →
  • 사용
  • 팁 & 트릭
  • 디자인 결정