记录一次异常的排查
// 这是yam-cpp源代码里面,YAML::LoadFile的方法 Node LoadFile(const std::string& filename) { std::ifstream fin(filename); if (!fin) { throw BadFile(filename); } return Load(fin); }
执行这行加载文件代码的时候,异常了:
YAML::Node root = YAML::LoadFile("/home/henry/workspace/henry-sylar/bin/conf/log2.yml");
异常信息如下:
[henry@192 bin]$ ./test_thread 2024-01-01 13:52:48 5670 0 [INFO] [root] tests/test_thread.cpp:37 thread test begin terminate called after throwing an instance of 'YAML::BadFile' what(): bad file: /home/henry/workspace/henry-sylar/bin/conf/log2.yml Aborted
首先,第一反应是 这里是抛出了异常。
其次,结合代码逻辑,我们定位到异常是 YAML::LoadFile() 这个方法执行之后抛出的。
然后我们去看这个LoadFile方法的实现细节。
最后我们定位到了 throw BadFile(filename) 这行。发现是日志路径有问题,LoadFile函数没有找到这个文件,然后抛出了异常。