当捕获boost::bad_exical_cast时,我可以访问要强制转换的字符串/令牌吗
When catching a boost::bad_lexical_cast, can I access the string/token which was to be cast?
我正在运行的代码可能会在强制转换一系列令牌时抛出boost:bad_lexical_cast
,但我不能进入代码并"将令牌放在一边",这样我就可以找出实际失败的强制转换。
boost:bad_lexical_cast
是否允许您访问它试图以某种方式强制转换的字符串?我似乎在它的定义中找不到任何东西,除了一些关于类型名的字段,但也许我缺少了一些东西。
如果您可以控制调用lexical_cast的代码,那么您可以使用函数try块来捕获、包装和重新抛出异常,并提供额外信息:
#include <boost/lexical_cast.hpp>
#include <string>
#include <stdexcept>
#include <exception>
struct conversion_error : std::runtime_error
{
conversion_error(const std::string& name, const std::string& value)
: std::runtime_error::runtime_error("converting " + name + " : " + value)
, name(name)
, value(value)
{}
const std::string name, value;
};
struct test
{
void bar()
{
try {
foo();
} catch(const conversion_error& e) {
std::cerr << e.what() << std::endl;
}
}
void foo()
try
{
i = boost::lexical_cast<int>(s);
}
catch(...)
{
std::throw_with_nested(conversion_error("s", s));
}
std::string s;
int i;
};
int main()
{
test a { "y", 0 };
a.bar();
}
相关文章:
- wcstombs_s(),转换字符串的长度
- 使用 CStringW/CStringA 和 CT2W/CT2A 转换字符串有什么区别?
- 无法转换字符串插入函数的参数
- 转换字符串向量:基于先前的值跳过元素
- 从中间器转换字符串不起作用,出了什么问题?
- 失败的证明转换字符串到枚举类
- 如何使用正则表达式和提升转换迭代器标记和转换 c 字符串
- 使用java jni时转换字符串类型
- 如何避免在转换字符串令牌流时重复istringstream构造
- 阿托伊未正确转换字符串
- C++,转换字符串,使连续下划线序列变为单个下划线
- 使用长算术转换字符串
- 转换字符串十进制数时的精度
- 将词法转换字符串提升为双精度
- 使用marshal_as函数转换字符串的性能
- C++不会从数据转换字符串
- 无论如何,在c++ /Qt中转换字符串到结构中的字段
- C++ 隐式类型转换字符串 -> int?
- 转换字符串到浮点数,c++实现
- 用于转换字符串大小写的函数