C++ 예외 처리의 위험성과 단점

2021. 3. 22. 00:13·SW개발/C++
반응형

예외 처리의 위험성과 단점

메모리 누수

  • throw 이후의 코드는 무시되기 때문에, 메모리 누수가 발생할 가능성이 존재한다.

    #include <iostream>
    
    using namespace std;
    
    int            main()
    {
      try
      {
        int* i = new int[100000];
    
        throw "error";
    
        delete[] i;
      }
      catch (...)
      {
        cout << "Catch\n";
      }
    }
    
    /* stdout stderr
    Catch
    */
    • unique_ptr를 사용하여 해결할 수 있다.

소멸자에서 throw 사용 금지

  • 소멸자에서는 throw의 사용이 금지되어있다.

    • 만약 사용하면 런타임 에러가 발생한다.
    #include <iostream>
    #include <memory>
    
    using namespace std;
    
    class A
    {
    public:
      ~A()
      {
        throw "error";
      }
    };
    
    int            main()
    {
      try
      {
        A a;
      }
      catch (...)
      {
        cout << "Catch\n";
      }
    }
    warning C4297: 'A::~A': function assumed not to throw an exception but does
    message : destructor or deallocator has a (possibly implicit) non-throwing exception specification
반응형
저작자표시 (새창열림)

'SW개발 > C++' 카테고리의 다른 글

C++ 스마트 포인터 (Smart Pointer)  (0) 2021.03.22
C++ 따라하며 배우는 C++ 15장  (0) 2021.03.22
C++ 함수 try (Function try)  (0) 2021.03.22
C++ std::exception  (0) 2021.03.22
C++ 예외 클래스와 상속 (Exception Class and Inheritance)  (0) 2021.03.22
'SW개발/C++' 카테고리의 다른 글
  • C++ 스마트 포인터 (Smart Pointer)
  • C++ 따라하며 배우는 C++ 15장
  • C++ 함수 try (Function try)
  • C++ std::exception
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 전체보기 (319)
      • SW개발 (268)
        • Java Spring (6)
        • C++ (186)
        • Python (21)
        • Linux (16)
        • 알고리즘 (13)
        • Git (4)
        • Embedded (1)
        • Raspberrypi (9)
        • React (3)
        • Web (2)
        • Windows Device Driver (6)
      • IT(개발아님) (46)
        • Windows (26)
        • MacOS (7)
        • Utility (11)
      • 챗봇 짬통 (0)
      • 일상 (2)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 예외 처리의 위험성과 단점
상단으로

티스토리툴바