这是我第一次处理异常,但在理解异常时遇到了一些问题。这是我的C ++作业。我需要你们的帮助。当我在此站点上向大家询问时,我总是会找到解决问题的方法。谢谢!
这是我运行程序时得到的输出:
An exception test is about to take place.
AnnounceDestroyed已创建。 AnnounceDestroyed被销毁。 -1
将要进行异常测试。 AnnounceDestroyed已创建。 AnnounceDestroyed被销毁。 错误!
将要进行异常测试。 AnnounceDestroyed已创建。 尝试了异常,但没有发生。 AnnounceDestroyed被销毁。 没有发生异常。
我在我的两个虚函数中使用了throw,它们似乎工作正常,但是当我在第三个函数中尝试使用它时,编译器给我一个错误。 如果向下滚动,则会在编译器给我错误的地方找到我的注释。 这是我的代码:
#include <iostream>
#include <memory>
using namespace std;
class AnnounceDestroyed
{
public:
AnnounceDestroyed(){std::cout << "AnnounceDestroyed is created." << std::endl;}
~AnnounceDestroyed(){std::cout << "AnnounceDestroyed is destroyed." << std::endl;}
};
class ExceptionThrower
{
public:
virtual void launchException() = 0;
virtual ~ExceptionThrower(){};
};
class IntThrower : public ExceptionThrower
{
private:
int thrwVar;
public:
IntThrower(int var){thrwVar = var;}
void launchException()
{
throw thrwVar;
}
};
class MsgThrower : public ExceptionThrower
{
private:
string msgStr;
public:
MsgThrower(string str){msgStr = str;}
void launchException()
{
throw msgStr;
}
};
class CustomThrower : public ExceptionThrower
{
private:
int customInt;
string customStr;
public:
CustomThrower(int cstmInt, string cstmString)
{customInt = cstmInt; customStr = cstmString;}
void launchException()
{
//throw customStr; // when I user throw here compiler gives me an error for some reasons.
// here is the error : "Thread 1: signal SIGABRT"
//throw customInt; // And here as well.
}
};
class CustomException
{
private:
int cstmExpInt;
string cstmExpStr;
public:
CustomException(int aInt, string bStr)
{cstmExpInt = aInt; cstmExpStr = bStr;}
void printContents()
{
std::cout << cstmExpStr << std::endl;
std::cout << cstmExpInt << std::endl;
}
};
void activateException(ExceptionThrower ¤tThrower)
{
shared_ptr<AnnounceDestroyed> announce = make_shared<AnnounceDestroyed>();
currentThrower.launchException();
std::cout << "An exception was attempted, but did not occur." << std::endl;
}
int main()
{
// Constants
const int INT_THROWER_CODE = -1;
const string MSG_THROWER_STRING = "Error!";
const int CUSTOM_THROWER_CODE = -99;
const string CUSTOM_THROWER_STRING = "Major error!";
int exceptionCounter = 0;
IntThrower LauncherInt(INT_THROWER_CODE);
MsgThrower LauncherMsg(MSG_THROWER_STRING);
CustomThrower LauncherCustom(CUSTOM_THROWER_CODE, CUSTOM_THROWER_STRING);
try {
std::cout << "An exception test is about to take place." << std::endl;
activateException(LauncherInt);
std::cout << "An exception did not take place." << std::endl;
}
catch (int &a)
{
std::cout << a << std::endl;
std::cout << std::endl;
}
try {
std::cout << "An exception test is about to take place." << std::endl;
activateException(LauncherMsg);
std::cout << "An exception did not take place." << std::endl;
}
catch (string &b)
{
std::cout << b << std::endl;
std::cout << std::endl;
}
try {
std::cout << "An exception test is about to take place." << std::endl;
activateException(LauncherCustom);
std::cout << "An exception did not take place." << std::endl;
std::cout << std::endl;
}
catch (CustomException &c)
{
c.printContents();
}
}
答案 0 :(得分:0)
您需要一个“ throw”子句来引发异常。
如果将'launchException'更改为:也许会得到想要的东西
void launchException() override
{
std::cout << customInt << std::endl;
std::cout << customStr << std::endl;
throw CustomException(customInt, customStr);
}
还要注意“ override”关键字。这可能有助于避免您在评论中提到的警告。