C++ 함수 try (Function try)

2021. 3. 22. 00:12·C++/Exception

함수 try (Function try)

함수의 body 자체가 try, catch로 이루어진 경우

  • 함수를 정의할 때 중괄호로 감싸지 않아도 된다고 한다.

  • 잘 쓰이는 문법은 아닌 것 같다.

  • rethrow를 하지 않는다.

    #include <iostream>
    
    using namespace std;
    
    void        doSomething()
    try
    {
      throw - 1;
    }
    catch (...)
    {
      cout << "Catch in doSomething()\n";
    }
    
    int            main()
    {
      try
      {
        doSomething();
      }
      catch (...)
      {
        cout << "Catch in main()\n";
      }
    }
    
    /* stdout stderr
    Catch in doSomething()
    */

클래스의 생성자에서 예외가 발생할 경우

  • 생성자의 멤버 초기화 리스트까지 포함하여 try를 시도하고, 바로 아래에서 catch한다.

  • 자동으로 rethrow를 적용한다.

    #include <iostream>
    
    using namespace std;
    
    class A
    {
      int    x_;
    
    public:
      A(int x) : x_(x)
      {
        if (x <= 0)
          throw 1;
      }
    };
    
    class B : public A
    {
    public:
      B(int x) try : A(x)
      {}
      catch (...)
      {
        cout << "Catch in B constructor\n";
        //throw;
      }
    };
    
    int            main()
    {
      try
      {
        B b(0);
      }
      catch (...)
      {
        cout << "Catch in main()\n";
      }
    }
    
    /* stdout stderr
    Catch in B constructor
    Catch in main()
    */
저작자표시 (새창열림)

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

C++ 예외 처리의 위험성과 단점  (0) 2021.03.22
C++ 예외 클래스와 상속 (Exception Class and Inheritance)  (0) 2021.03.22
C++ 스택 되감기 (Stack Unwinding)  (0) 2021.03.22
C++ 예외 처리의 기본  (0) 2021.03.22
C++ 예외 처리 (Exception Handling)  (0) 2021.03.22
'C++/Exception' 카테고리의 다른 글
  • C++ 예외 처리의 위험성과 단점
  • C++ 예외 클래스와 상속 (Exception Class and Inheritance)
  • C++ 스택 되감기 (Stack Unwinding)
  • C++ 예외 처리의 기본
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)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 함수 try (Function try)
상단으로

티스토리툴바