[c++]exception

tahn·2022년 7월 5일
0

C++

목록 보기
2/8
post-thumbnail

C++은 여러 예외 클래스의 기초 클래스로 사용할 수 있는 exception 클래스를 제공합니다.

exception 클래스는 시스템에 따라 하나의 문자열 포인터를 반환하는 what()이라는 가상 멤버 함수를 제공합니다.

이 멤버 함수는 가상 함수이므로, exception 클래스로부터 파생된 클래스 내에서 재정의할 수 있습니다.

exception 클래스의 what() 멤버 함수는 별다른 일을 하지는 않지만, 파생 클래스에서는 원하는 문자열을 출력할 수 있도록 재정의할 수 있습니다.

exception 클래스의 정의는

  1. logic_error
  2. runtime_error

logic_error 클래스는 일반적인 논리에 관한 오류들을 처리할 수 있습니다.

runtime_error 클래스는 프로그램이 실행하는 동안 발생할 수 있는 다양한 오류들을 처리할 수 있습니다.

처리되지 않은 예외

C++ 프로그램은 발생한 예외를 처리할 catch 절을 찾을 수 없을 때, 미리 정의된 terminate() 함수를 호출합니다.

이 terminate() 함수는 기본적으로 abort() 함수를 호출하여 프로그램을 강제로 종료시킵니다.

try_catch 에서 실 사용 예

try
    {
        stmt = con->createStatement();

        char buf[256] = { 0, };
        sprintf_s(buf, 256, "SELECT * FROM pizzaorder.accounts");

        res = stmt->executeQuery(buf);
        while (res->next()) 
        {
            const auto account = res->getString("account");
            const auto pw = res->getString("pw");
            const auto type = res->getInt("type");

            cout << "[DBConnector] account = " << account << " pw = " << pw << " type = " << type << endl;

            AccInfo accInfo(account.asStdString(), pw.asStdString(), (EN_LOGIN_TYPE)type);
            GenAcc.push_back(accInfo);
        }

        res->close();
        stmt->close();

        delete res;
        delete stmt;

        return !GenAcc.empty();
    }
    catch (exception e)
    {
        res->close();
        stmt->close();

        if ( res)
            delete res;

        if ( stmt)
            delete stmt;

        return false;
    }

레퍼런스 : tcp class

profile
html 개발자

0개의 댓글