小编Cha*_*e M的帖子

如何创建例外?

所以我有一个即将到来的任务处理异常并在我当前的地址簿程序中使用它们,大多数功课都集中在这里.我决定玩异常和整个尝试捕获的东西,并使用类设计,这是我最终将在几周内完成我的任务.我有工作代码检查异常就好了,但我想知道的是,是否有办法标准化我的错误消息功能,(即我的what()调用):

这是我的代码:

#include <iostream>
#include <exception>
using namespace std;


class testException: public exception
{
public:
  virtual const char* what() const throw() // my call to the std exception class function (doesn't nessasarily have to be virtual).
  {
  return "You can't divide by zero! Error code number 0, restarting the calculator..."; // my error message
  }

  void noZero();

}myex;  //<-this is just a lazy way to create an object



int main()
{
void noZero();
int a, b;

cout << endl;

cout …
Run Code Online (Sandbox Code Playgroud)

c++ exception custom-exceptions

20
推荐指数
1
解决办法
7万
查看次数

标签 统计

c++ ×1

custom-exceptions ×1

exception ×1