C++ std::exception

2021. 3. 22. 00:11·C++/Library

std::exception

  • <exception> 라이브러리

exception 클래스

  • 자식으로 여러 클래스를 가지고 있다.

  • std::exception::what() 함수를 통해 예외 상황을 알 수 있다.

    • 다음과 같이 exception 클래스에서는 가상함수로 정의되어있다.

      _NODISCARD virtual char const* what() const
      {
          return _Data._What ? _Data._What : "Unknown exception";
      }
    • https://en.cppreference.com/w/cpp/error/length_error 에 상속 다이어그램이 있으니 참고하면 좋다.

    #include <iostream>
    #include <exception>
    
    using namespace std;
    
    int            main()
    {
      try
      {
        string    s;
        s.resize(-1);
      }
      catch (std::exception & exception)
      {
        cout << typeid(exception).name() << endl;
        cerr << exception.what() << endl;
      }
    }
    
    /* stdout stderr
    class std::length_error
    string too long
    */
  • 직접 자식 클래스를 던질 수도 있다.

    #include <iostream>
    #include <exception>
    
    using namespace std;
    
    int            main()
    {
      try
      {
        throw std::runtime_error("Bad thing happened");
      }
      catch (std::exception & exception)
      {
        cout << typeid(exception).name() << endl;
        cerr << exception.what() << endl;
      }
    }
    
    /* stdout stderr
    class std::runtime_error
    Bad thing happened
    */

직접 상속받는 클래스 만들기

  • std::exception 클래스를 상속받아 새로운 클래스를 정의하고 이를 사용할 수 있다.

    • what 함수를 오버라이딩해야 의미가 있다.

    • noexcept

      • C++11

      • 예외를 던지지 않는다는 뜻이다. 없어도 되는 것 같다.

    #include <iostream>
    #include <exception>
    
    using namespace std;
    
    class CustomException : public std::exception
    {
    public:
      const char* what() const noexcept override
      {
        return "Custom exception";
      }
    };
    
    int            main()
    {
      try
      {
        throw CustomException();
      }
      catch (std::exception & exception)
      {
        cout << typeid(exception).name() << endl;
        cerr << exception.what() << endl;
      }
    }
    
    /* stdout stderr
    class CustomException
    Custom exception
    */
저작자표시 (새창열림)

'C++ > Library' 카테고리의 다른 글

C++ std::move  (0) 2021.03.24
C++ 출력 스트림 끊기  (0) 2021.03.24
C++ reference_wrapper  (0) 2021.03.21
C++ IntArray  (0) 2021.03.19
C++ initializer_list  (0) 2021.03.19
'C++/Library' 카테고리의 다른 글
  • C++ std::move
  • C++ 출력 스트림 끊기
  • C++ reference_wrapper
  • C++ IntArray
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (317)
      • Algorithm (13)
        • 알기 쉬운 알고리즘 (10)
        • Search (1)
        • Sort (2)
      • Arduino (0)
      • C++ (185)
        • Class (46)
        • Exception (6)
        • Library (51)
        • Overloading (10)
        • SmartPointer (5)
        • Syntax (33)
        • TBC++ (23)
        • Templates (9)
        • VisualStudio (2)
      • Embedded (1)
      • Git (4)
      • Java (5)
      • Linux (16)
        • Error (1)
        • Linux Structure (11)
      • MacOS (7)
      • OS (1)
        • Concurrency (1)
      • Python (21)
        • Class (1)
        • Function (2)
        • Syntax (17)
      • Raspberrypi (9)
      • Review (1)
      • Utility (12)
        • VSCode (5)
        • VirtualBox (3)
      • Web (8)
        • Nginx (1)
        • React (3)
        • Django (1)
      • Windows (20)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    mspaint
    로지텍 마우스 제스처
    citrix workspace
    백기선
    알림
    윈도우 명령어
    dism
    Windows 11
    스프링
    logi options
    SunOS 5.1
    윈도우
    그림판
    스프링 프레임워크 핵심 기술
    Workspace
    java
    시스템 복구
    Solaris 10
    MacOS
    KakaoTalk
    SFC
    제외
    맥북 카카오톡 알림 안뜸
    vscode
    EXCLUDE
    unix
    windows
    spring
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ std::exception
상단으로

티스토리툴바