C++ 예외 처리의 기본

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

예외 처리의 기본

try, throw, catch

  • try : 코드를 실행할 부분

  • throw : 예외 상황이 발생하면 해당 정보를 던지는 부분

  • catch : try 내부에서 throw가 발생했을 경우 이를 받아주는 부분

    #include <iostream>
    
    using namespace std;
    
    int            main()
    {
      double    x;
      cin >> x;
    
      try
      {
        if (x < 0.0)
          throw string("Negative input");
        cout << std::sqrt(x) << endl;
      }
      catch (string error_message)
      {
        cout << error_message << endl;
      }
    }
    
    /* stdin
    -10
    */
    
    /* stdout
    Negative input
    */

  • 묵시적 형변환이 허용되지 않는다.

    ex) std::string -> const char* 불가능

    #include <iostream>
    
    using namespace std;
    
    int            main()
    {
      double    x;
      cin >> x;
    
      try
      {
        if (x < 0.0)
          throw "Negative input";  // 런타임 에러
        cout << std::sqrt(x) << endl;
      }
      catch (string error_message)
      {
        cout << error_message << endl;
      }
    }

주의사항

  • 연산량이 많아서 처리속도가 느려지므로, 성능이 중요한 프로그램에서는 가급적 사용을 지양해야 한다.

    • 조건문으로 거를 수 있는 예외 상황은 조건문을 사용해서 처리하는게 좋다.

    • 예측 불가능한 예외 사항에 대해서만 사용하는게 좋다.

저작자표시 (새창열림)

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

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 예외 처리의 기본
상단으로

티스토리툴바