博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

[c++] c++ exception

Posted on 2010-06-07 19:39  xuczhang  阅读(1415)  评论(0编辑  收藏  举报

 

We will start from an example to illustrate the c++ exception. Two classes is in the example: Person and Student. class Student is the child class of class Person.

class Person
{
};
 
class Student : public Person
{
};
 
void func()
{
    
    Student student;
    try
    {
        throw student;    // copy student object as exception object
    }
    catch (Student& s)    // use the exception object as a reference
    {
        std::cout << "catch Student& s" << std::endl;
    }
    catch (...)
    {
        std::cout << "catch Exception e" << std::endl;
    }
}

 

Points:

1.  exception object

The phrase “exception object” means the object throwed throughout the exception. In our example, it’s “student” object in the beginning of function func(). The object is in the charge of compiler and it can be accessed by any catch. The object is created by throw statment, and it’s the copy of the original object.

2.  difference between catch reference and object

Below is the call sequence of construction and copy construction in the 2 conditions:

catch reference (Student& s)

image

catch object (Student s)

image

We can see that the difference is “catch object” copies the exception object, but “catch reference” doesn’t. So if you want to change the exception object, then throw it again, you should use “catch reference” instead.

Such as:

catch (Student& s)
{
    // change the exception object
    ...
    throw;
}

 

3. inheretance in exception

1) exception object

Student student;
Person& p = student;

throw p;

if throw the super class’s reference or dereference of pointer, only the super class part is copied!!!

In the example, “throw p” only copy the Person part of class Student, so it won’t match the Student type catch statment.

Student student;
Person& p = student;
try
{
    throw p;
}
catch (Person& s)
{
    std::cout << "catch Student s" << std::endl;
    s.Hello();
}
catch (...)
{
    std::cout << "catch exception" << std::endl;
}

image

4. function try block

function try block is the only solution to deal with the exception from construction initializer.

template<class T>
Handle<T>::Handle(T* p)
try : ptr(p), use(new size_t(1))
{
    // empty function body
}
catch(const std::bad_alloc& e)
{
    // handle statement
}

 

5. exception specification

 
void no_problem throw(); // no exception throw
 
void recoup(int) throw (runtime_error); // throw runtime error exception
 
void recoup(int) throw (runtime_error,logic_error); // throw runtime error and logic error exception

1) exception specification is to declare what exception will be throwed in the function. It will be warned by compiler when the rule is broken(c++ primer says it will terminate in run time. But in vs2005, it gives the warning when compiling).

2) The child class’s function exception specification should be less restrict than the super class’s.

In other words, the exception specifications of super class should include the child class

3) exception specification is a part of function declaration. So the assignment of two function declaration with different exception specification is incorrect.

void recoup(int throw(runtime_error);
void (*pf)(int throw(runtime_error, logic_error) = recoup; // error