我应该C++自定义异常代码放在哪里?
Where do I put C++ Custom Exception Code?
我正在为我编写的生成随机短语的类编写一个自定义异常。我是C++新手,我想知道我是否应该将异常放在类头文件、类.cpp文件中,或者我是否需要拆分声明和实现。
Eclipse 在 main(( 方法中给了我一个错误,指出:
error: 'FileException' does not name a type
} catch (FileException& e) {
异常类如下所示:
class FileException : public std::exception {
public:
const char* what() {
return "File Could not be opened.";
}
}FileException;
对这个问题的任何想法将不胜感激,因为我非常困和困惑。
谢谢!
编辑:我还应该提到,我只应该提交2个文件,类的.cpp文件和.h文件
去掉变量(你可能不需要它(或为类和变量使用不同的名称。
解释:
class FileException : public std::exception {
FileException
是一堂课。很酷。
public:
const char* what() {
return "File Could not be opened.";
}
}FileException;
最后一位定义了一个名为FileException
的FileException
类型的变量,用于替换FileException
类。这与定义相同
class FileException : public std::exception {
public:
const char* what() {
return "File Could not be opened.";
}
};
FileException FileException;
标识符FileException
现在引用变量,而不能引用类。
相关文章:
- 我的重复检查代码中的错误究竟在哪里?
- 我在哪里修复有关模板的C++代码?
- 我在哪里编写在退出 C++ 窗口窗体应用程序之前执行的代码部分
- 此代码中的右值引用在哪里?
- 这些代码之间的区别在哪里?输出不应该是一样的吗?
- Microsoft计算器中的代码在哪里添加两个数字
- 我的代码到底在哪里不符合键和值类型的规范
- 此代码给出错误的输出无法找出我在哪里犯了错误
- 这个代码的瓶颈在哪里
- 在哪里放置重载<<代码?
- 此(C )代码有什么问题吗?如果是这样,有人可以告诉我我在哪里出错
- 在哪里可以在ASIO的源代码中获取有关IOCP的代码
- Win32 控制台代码在哪里运行?从多个线程使用时控制台挂起
- 在哪里可以找到C++中流行的视频游戏代码
- 我的代码中的错误在哪里
- C++在哪里可以获得sha256 openssl的代码版本
- 我在哪里定义代码来释放在C Python模块init函数中分配的资源
- 我应该在哪里保存代码块项目
- C++外部程序集:我的代码中的错误在哪里
- 此代码中的分段错误在哪里