尝试/捕获的奇怪行为
Odd behavior of try/catch
本文关键字:尝试 更新时间:2023-10-16
我的问题是:为什么下面的代码:
class A
{
public:
A()
{
test[0] = "three";
test[5] = "five";
test2[3] = 5.55;
}
void foo(int j)
{
for(int i = j+1; i <= 7; ++i)
{
try
{
std::cout<<test.at(i)<<"n";
}
catch(const std::out_of_range&)
{
try
{
std::cout<<test2.at(i)<<"n";
}
catch(const std::out_of_range&)
{
throw i;
}
}
}
}
virtual void bar()
{
}
std::map< int, float > test2;
std::map<int, std::string> test;
};
class B : public A
{
public:
B()
{
test3[6] = 15;
test3[7] = 42;
bar();
}
void bar()
{
int k = -1;
label:
try
{
foo(k);
}
catch(int i)
{
try
{
std::cout<<test3.at(i)<<"n";
}
catch(const std::out_of_range&)
{
k = i;
goto label;
}
}
}
std::map<int, int> test3;
};
打印
three
5.55
five
15
而不是
three
5.55
five
15
42
?
我正在尝试做的是迭代包含不同数据类型的地图,这些地图不能保存在一个容器中,这就是我想出的
我的理解是,你需要的是:
- 您希望通过一定范围的键打印不同映射中包含的所有值。
- 地图具有不同类型的值。并非所有键都存在于所有映射中。
- 派生类可能包含其他映射。
与其使用这种复杂的 exception+goto 设计,为什么不使用基于虚拟方法的更简单的设计来打印特定值:
class A {
public:
virtual void showValue(int key) {
if (map1.count(key))
std::cout << map1[key];
else if (map2.count(key))
std::cout << map2[key];
}
void showAll() {
for (int i=0; i<=7; i++)
showValue(i);
}
std::map<int, float> map1;
std::map<int, std::string> map2;
};
class B : public A {
public:
virtual void showValue(int key) {
if (map3.count(key))
std::cout << map3[key];
else
A::showValue(key);
}
std::map<int, int> map3;
};
正在发生的事情是,当您打印 15 时,您throw i
.这不会被捕获,并逃逸到:
catch(int i)
{
try
{
std::cout<<test3.at(i)<<"n";
}
catch(const std::out_of_range&)
{
k = i;
goto label;
}
}
该号码已正确打印在那里,但不会重新启动。在不知道更多细节的情况下,弄清楚如何解决它几乎是不可能的......
更好的解决方案如下所示:
for (int i=0;i<=7;i++)
{
if (test.find(i)!=std::map::end)
std::cout<<test.at(i)<<"n";
else if (test2.find(i)!=std::map::end)
std::cout<<test2.at(i)<<"n";
else if (test3.find(i)!=std::map::end)
std::cout<<test3.at(i)<<"n";
else
std::count<<"Nothing Found"<<std::endl;
}
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 正在尝试了解输入验证循环
- 尝试通过多个向量访问变量时,向量下标超出范围
- 尝试导入pybind-opencv模块时出现libgtk错误
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- 我正在尝试使用 c++ 创建一个货币转换程序,我不知道如何继续
- 正在尝试重载二进制搜索树分配运算符
- 尝试使用集合函数时出现分段错误
- 如何在c++中限制尝试次数
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 尝试将unique_ptrs推送到向量时使用纯虚拟函数错误
- 尝试将lambda函数放在队列中时出现一般分配器错误(可能是与unique_ptr有关的错误)
- '尝试解析可变参数模板时无法推断出'T的模板参数
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- Visual Studio在尝试读取resource.txt文件时崩溃