[c++] c++ exception

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.

  1. 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;
}
  1. 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

  1. 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
}
  1. 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

原文链接: https://www.cnblogs.com/xuczhang/archive/2010/06/07/1753433.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/11581

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 上午10:14
下一篇 2023年2月7日 上午10:16

相关推荐