Create TOC

2001년 3월 8일

C++/Exceptions

STL에서 exception

header file : <stdexcept>

Exception Classes

domain_errorClass The class serves as the base class for all exceptions thrown to report a domain error.
invalid_argumentClass The class serves as the base class for all exceptions thrown to report an invalid argument.
length_errorClass The class serves as the base class for all exceptions thrown to report an attempt to generate an object too long to be specified.
logic_errorClass The class serves as the base class for all exceptions thrown to report errors presumably detectable before the program executes, such as violations of logical preconditions.
out_of_rangeClass The class serves as the base class for all exceptions thrown to report an argument that is out of its valid range.
overflow_errorClass The class serves as the base class for all exceptions thrown to report an arithmetic overflow.
range_errorClass The class serves as the base class for all exceptions thrown to report a range error.
runtime_errorClass The class serves as the base class for all exceptions thrown to report errors presumably detectable only when the program executes.
underflow_errorClass The class serves as the base class for all exceptions thrown to report an arithmetic underflow.

example

#include <iostream>

using namespace std;

int main( )
{
    // runtime_error
    try
    {
        locale loc( "test" );
    }
    catch ( exception &e )
    {
        cerr << "Caught " << e.what( ) << endl;
        cerr << "Type " << typeid( e ).name( ) << endl;
    };
}

try..catch example code

class Widget 이 있다고 하면,

case 1.

try
{
    Widget w1;          // default constructor is called(object 1)
    throw w1;           // copy constructor is called.(object 1->object 2)
}                       // destructor is called(object 1)
catch (Widget w)        // copy constructor is called(object 2->object3)
{
    throw;              // re-throw object2
}                       // destructor is called(object3)

case 2.

try
{
    Widget w1;          // default constructor is called(object 1)
    throw w1;           // copy constructor is called.(object 1->object 2)
}                       // destructor is called(object 1)
catch(Widget& w)        // copy constructor is not called(reference)
{
    throw;              // re-throw object2
}                       // destructor is not called.

case 3.

try
{
    Widget w1;          // default constructor is called(object 1)
    throw w1;           // copy constructor is called.(object 1->object 2)
}                       // destructor is called(object 1)
catch(Widget& w)        // copy constructor is not called(reference)
{
    throw w;            // copy constructor is called.(object 2->object 3)
}                       // destructor is called(object 2.).

case 4. Effective code

try
{
    throw Widget();     // default constructor is called(object1). copy constructor is called.(object 1->object2).
                        // or no copy construction by a well-known optimization.
}                       // destructor is call(object1) or not by a well-known optimization.
catch(Widget& w)        // copy constructor is not called(reference)
{
    throw;              // re-throw object2(or 1)
}                       // destructor is not called(object 2. or 1).

foo() throw() 구문

foo() throw()

foo() 함수 안에서는 어떠한 exception 도 발생되지 않는다. 만일 exception 이 발생될 경우 std::unexpected() exception이 발생되며 std::unexpected() 의 처리자를 지정하지 않으면 기본으로 std::terminated() 가 호출되고, std::terminated() 에서는 abort() 함수를 호출한다.

foo() throw(...)

foo() 함수에서 임의의 exception 이 발생될 수 있다.

foo() throw(a, b)

foo() 함수에서 a, b 2개의 exception이 발생될 수 있다. 그 외의 exception이 발생하면 std::unexpected() exception이 발생된다.