c++ - std::rethrow_exception with std::current_exception - Stack Overflow
Just fumbling around with exceptions for no further reason. In C++20, I noticed a behaviour around the std::rethrow_exception
function when used with nested exceptions and std::current_exception
.
Consider this example:
#include <iostream>
#include <exception>
using namespace std;
int main()
{
struct B {
B(int ii): i(ii) { }
virtual ~B() { }
int i;
};
try {
try {
throw B(1);
}
catch (const B &b) {
std::throw_with_nested(B(2));
}
}
catch (const B &b) {
cout << "B" << b.i << endl;
try {
std::rethrow_if_nested(b); // works
//std::rethrow_if_nested(std::current_exception()); // doesn't work
}
catch (const B &b2) {
cout << "B" << b2.i << endl;
}
}
}
If I rethrow the exception b
itself, I get the following output:
B2
B1
This is what I expected. The outer catch
block gets the outer exception, then rethrows it so that the second catch
block gets the inner/nested exception.
However, when I change the commented lines, I suddenly only get B2
and the inner/nested exception seems to be lost. This also happens e.g., when the exception is captured in an std::exception_ptr
midway.
How is that?
最新文章
- 下个月Win7正式“退休”,数据显示国内近60%电脑用户仍在使用
- 甲骨文:安卓让谷歌赚了220亿
- 恶意软件侵害苹果用户!4G更易受攻击
- javascript - Chrome Extension with proxy, onAuthRequired never being triggered - Stack Overflow
- ST_CONTAINS() giving FALSE even if Point lies within polygon. google-bigquery - Stack Overflow
- pytorch - Why can't my DDPG solve MountainCarContinuous-v0 - Stack Overflow
- flutter - App Name Not Updating in Android Recent Apps View Despite Manifest and Strings Configuration - Stack Overflow
- You don’t have permission to view or edit anything. Django Admin. Web-site for school - Stack Overflow
- intellij http client - How to use in place variable within handler scripts? - Stack Overflow
- amazon ses - Is it possible to send a RawMessage using Apache Camel AWS SES? - Stack Overflow
- xamarin - How to add static library(.a) in to .Net Maui iOS application - Stack Overflow
- django - SimpleJWT: Re-apply Blacklist Token Migration: "Table 'token_blacklist_blacklistedtoken' doesn
- apple swift: is forKey:fileSize considered accessing non-public API? - Stack Overflow
- Why is my bot not able to detect when a user leaves in Telegram? - Stack Overflow
- google apps script - How do I correct my code to move a row from one tab to another tab in Sheets, and then delete it from the s
- c# - Unity build error for Android when use CMake with Ninja on Mac - Stack Overflow
- class - (Python) cant access attribute of an object unless I check nullity, why? - Stack Overflow