尝试/捕获崩溃 255
Try/catch crash 255
尝试/捕获的新手,需要将其用于程序。出于某种原因,尽管我的程序在捕获 obj 时崩溃。我编写的示例程序只是读取一个数字,然后测试以查看它是低于 10 还是高于 20。关键是使用两个用户定义的类来抛出和捕获。上面的 20 obj 被扔掉并接住就好了。但是低于10个,如果它抛出会崩溃程序。这是为什么呢?这是我的代码
#include <iostream>
#include <cstdlib>
using namespace std;
class above20
{
public:
above20()
{
msg = "Number is above 20.";
};
string getmsg()
{
cout<<msg<<endl;
};
private:
string msg;
};
class below10
{
public:
below10()
{
msg = "Number is below 10";
};
string getmsg()
{
cout<<msg<<endl;
};
private:
string msg;
};
int main ()
{
int num;
try
{
cout<<"Enter Number between 10 and 20:"<<endl;
cin >> num;
if (num > 20)
{
throw above20 ();
}
}
catch (above20 obj)
{
obj.getmsg();
}
try
{
if (num < 10)
{
throw below10 ();
}
}
catch (below10 obj)
{
obj.getmsg();
}
system ("pause");
return (0);
}
你确定这是编译的吗? 您是否在复制粘贴中省略了某些内容? getmsg() 方法不返回任何内容。
---编辑---试试这个:
void getmsg()
{
cout<<msg<<endl;
};
你的代码中有很多糟糕的语法。 您得到的错误是因为您使用返回值声明getMsg
并且不返回(UB - 您很幸运它甚至可以编译!
要解决所有问题:http://ideone.com/1qGAuR
你的代码中有一些错误,比如有一个函数不返回任何内容,尽管它应该在签名中:
string getmsg()
{
cout<<msg<<endl;
};
真的应该是:
void getmsg()
{
cout<<msg<<endl;
};
或
string getmsg()
{
return string(msg);
};
暂时搁置这些错误,从设计的角度来看,从一个异常基类继承是更干净的。我通常会从std::runtime_error
继承,但如果你愿意,你可以定义自己的。例如:
#include <iostream>
#include <cstdlib>
using namespace std;
class above_below_exception
{
public:
virtual string getmsg() =0;
};
class above20 : public above_below_exception
{
public:
above20()
{
msg = "Number is above 20.";
};
string getmsg()
{
return string(msg);
};
private:
string msg;
};
class below10 : public above_below_exception
{
public:
below10()
{
msg = "Number is below 10";
};
string getmsg()
{
return string(msg);
};
private:
string msg;
};
int main ()
{
int num;
try
{
cout<<"Enter Number between 10 and 20:"<<endl;
cin >> num;
if (num > 20)
{
throw above20();
}
if (num < 10)
{
throw below10();
}
}
catch (above_below_exception& obj)
{
cout << obj.getmsg();
}
return (0);
}
代码的潜在修复:
#include <iostream>
#include <cstdlib>
using namespace std;
class above20 : public std::exception
{
public:
above20()
{
msg = "Number is above 20.";
}
virtual ~above20 () throw ()
{
}
string getmsg ()
{
cout << msg << endl;
return msg;
}
private:
string msg;
};
class below10 : public std::exception
{
public:
below10()
{
msg = "Number is below 10";
}
virtual ~below10 () throw ()
{
}
string getmsg()
{
cout << msg << endl;
return msg;
}
private:
string msg;
};
int main ()
{
int num;
try
{
cout<<"Enter Number between 10 and 20:"<<endl;
cin >> num;
if (num > 20)
{
throw above20 ();
}
}
catch (above20 &obj)
{
cout << obj. getmsg () << endl;
}
try
{
if (num < 10)
{
throw below10 ();
}
}
catch (below10 obj)
{
obj.getmsg();
}
system ("pause");
return (0);
}
相关文章:
- Visual Studio在尝试读取resource.txt文件时崩溃
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- For 循环在尝试读取数组 c++ 时程序崩溃
- 矢量迭代器在尝试调用函数时使我的程序崩溃
- 无法避免的libxml2内存泄漏是由于尝试清理时崩溃造成的
- 我的单例中的数组在离开函数后没有保留信息,然后在尝试再次访问信息时崩溃
- 在C++中实现二叉搜索树,搜索不起作用.尝试打印节点的元素会导致输出崩溃
- 当我尝试将一个向量元素的值分配给另一个向量元素时,为什么我的应用程序会崩溃
- 程序在 scanf( "%x" ,&foo) 上崩溃时尝试输入十六进制值
- C++ 尝试向类内的数组(或向量)添加值时程序崩溃
- 尝试获取数组中的最小元素时崩溃
- 尝试从另一个过程读取32位整数时,ReadProcessMemory崩溃了
- 尝试编写自定义Allocate_Shared分配器并将其变为thread_local时崩溃
- 矢量尝试添加字符串时会崩溃
- 我的C 代码在尝试计算表中元素的总和时崩溃
- 程序在尝试创建和访问二维矢量时崩溃
- 当我尝试删除指针时,我的程序崩溃
- 虚幻引擎4.20在我尝试玩游戏时崩溃
- 尝试打开 SDL2 窗口时与 D-Bus 相关的运行时崩溃
- 代码崩溃.尝试从字符数组 C 中删除字符