抛出异常但没有陷入c ++

时间:2018-06-02 08:47:42

标签: c++ visual-studio

嘿,我已经得到了相同的运行时错误很长一段时间,这是一个例外我投掷没有被捕获虽然我在尝试后得到一个捕获,这里是代码:

Company* Company::operator+=(Employee* emp){

if ((emp == NULL)) { //given a pointer to an element emp we check if the emp is NULL
    throw IllegalArguments();//we throw an error if it is null
}
if ((this->contains(emp->getID()) == false)) // we check if the Employee already exists in the Company
    _company.push_back(emp->clone());//a method to copy the found element
return this;

}

IllegalArguments是我为抛出错误定义的一个类,没有什么特别的,这是它的定义:

 #ifndef _ILLEGALARGUMENTS_
 #define _ILLEGALARGUMENTS_
 #include <iostream>
 #include <string>
 class IllegalArguments {};
 #endif // !_ILLEGALARGUMENTS_

所以我的所有代码都编译得非常好,除了主要的这些行:

 try {
        *company += NULL;
     }
     catch (IllegalArguments exp) {
        cout << "IllegalArguments exception was thrown !!" << endl;
      }

所以项目在catch的行停止并给我这个错误信息:

在Projectinfinity.exe中的0x74C0D722处抛出异常:Microsoft C ++异常:内存位置0x0096EA13处的IllegalArguments。

所以我的想法是公司是一个向量,它在每个索引中包含一个指向员工的指针,这是我定义的类,而运算符+ =是将一个员工添加到向量中,所以我检查是否要添加指针如果它为null则返回null然后我抛出一个错误,但显然看起来它不能处理catch事件,尽管我按值抛出异常并按值捕获。

0 个答案:

没有答案
相关问题