C++ 스택 되감기 (Stack Unwinding)

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

스택 되감기 (Stack Unwinding)

  • throw를 통해 던질 경우, 타입에 맞는 catch를 만날 때까지 스택을 되감는다.

    • throw 뒤의 코드는 실행하지 않는다.

    • 마찬가지로 catch를 만나지 못해서 스택을 되감는 경우에도 뒤의 코드를 실행하지 않고 바로 리턴한다.

    • 아래 코드를 디버깅하여 한 줄씩 보면 이해하기 편하다.

    #include <iostream>
    
    using namespace std;
    
    void        last()
    {
      cout << "Last function\n";
      cout << "Throw exception\n";
    
      throw - 1;
    
      cout << "End Last\n";
    }
    
    void        third()
    {
      cout << "Third function\n";
    
      last();
    
      cout << "End Third\n";
    }
    
    void        second()
    {
      cout << "Second function\n";
    
      try
      {
        third();
      }
      catch (double)
      {
        cerr << "Second caught int exception\n";
      }
    
      cout << "End Second\n";
    }
    
    void        first()
    {
      cout << "First function\n";
    
      try
      {
        second();
      }
      catch (int)
      {
        cerr << "First caught int exception\n";
      }
    
      cout << "End First\n";
    }
    
    int            main()
    {
      cout << "Main Start\n";
    
      try
      {
        first();
      }
      catch (int)
      {
        cerr << "Main caught int exception\n";
      }
    
      cout << "End Main\n";
    }
    
    /* stdout stderr
    Main Start
    First function
    Second function
    Third function
    Last function
    Throw exception
    First caught int exception
    End First
    End Main
    */

Ellipses

  • 생략 부호(...)를 사용하여 명시하지 않은 예외들에 대한 처리를 할 수 있다.

    #include <iostream>
    
    using namespace std;
    
    void        last()
    {
      cout << "Last function\n";
      cout << "Throw exception\n";
    
      throw 'a';
    
      cout << "End Last\n";
    }
    
    void        third()
    {
      cout << "Third function\n";
    
      last();
    
      cout << "End Third\n";
    }
    
    void        second()
    {
      cout << "Second function\n";
    
      try
      {
        third();
      }
      catch (double)
      {
        cerr << "Second caught int exception\n";
      }
    
      cout << "End Second\n";
    }
    
    void        first()
    {
      cout << "First function\n";
    
      try
      {
        second();
      }
      catch (int)
      {
        cerr << "First caught int exception\n";
      }
    
      cout << "End First\n";
    }
    
    int            main()
    {
      cout << "Main Start\n";
    
      try
      {
        first();
      }
      catch (int)
      {
        cerr << "Main caught int exception\n";
      }
      catch (...)
      {
        cerr << "Main caught ellipses exception\n";
      }
    
      cout << "End Main\n";
    }
    
    /* stdout stderr
    Main Start
    First function
    Second function
    Third function
    Last function
    Throw exception
    Main caught ellipses exception
    End Main
    */

Exceiption Specifier

  • 함수를 정의할 때 body 전에 throw(자료형)의 형식으로 작성하여 예외를 던질 가능성이 있는 함수임을 명시할 수 있다.

    void        last() throw(...)
    {
      cout << "Last function\n";
      cout << "Throw exception\n";
    
      throw 'a';
    
      cout << "End Last\n";
    }
  • throw() 처럼 파라미터를 작성하지 않을 경우에는 "예외를 던지지 않는다" 라는 뜻으로 사용된다.

    void        last() throw()
    {
      cout << "Last function\n";
      cout << "Throw exception\n";
    
      throw 'a';
    
      cout << "End Last\n";
    }
    warning C4297: 'last': function assumed not to throw an exception but does
저작자표시 (새창열림)

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

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

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 스택 되감기 (Stack Unwinding)
상단으로

티스토리툴바