
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
78 lines
1.7 KiB
Plaintext
78 lines
1.7 KiB
Plaintext
== Why is this an issue?
|
|
|
|
``++bool std::uncaught_exception()++`` allows you to know whether a thread is in an exception stack unwinding context. However, its practical functionality was restricted.
|
|
|
|
{cpp}17 deprecates ``++bool std::uncaught_exception()++`` and introduces ``++int std::uncaught_exceptions()++`` which returns the number of uncaught exceptions. The code example below shows how you can benefit from this new improved function.
|
|
|
|
|
|
``++std::uncaught_exception++`` has been removed in {cpp}20.
|
|
|
|
This rule will flag any usage of ``++std::uncaught_exception++``.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,cpp]
|
|
----
|
|
class Transaction {
|
|
|
|
// ...
|
|
|
|
~Transaction() {
|
|
if (!std::uncaught_exception()) { // Noncompliant, replace std::uncaught_exception by std::uncaught_exceptions
|
|
// commit
|
|
} else {
|
|
// rollback
|
|
}
|
|
}
|
|
};
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
The following example shows how ``++std::uncaught_exceptions++`` can be used to determine in ``++~Transaction++`` if a new exception was thrown since ``++t1++``/``++t2++`` creation.
|
|
|
|
[source,cpp]
|
|
----
|
|
class Transaction {
|
|
|
|
// ...
|
|
|
|
~Transaction() {
|
|
if (initialUncaughtExceptions == std::uncaught_exceptions()) {
|
|
// commit
|
|
} else {
|
|
// rollback
|
|
}
|
|
}
|
|
|
|
// ...
|
|
|
|
int initialUncaughtExceptions = std::uncaught_exceptions();
|
|
};
|
|
|
|
int f() {
|
|
try {
|
|
Transaction t1;
|
|
// ... something here could throw
|
|
} catch(...) {
|
|
Transaction t2;
|
|
// ... something here could throw
|
|
}
|
|
}
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Replace this use of std::uncaught_exception by std::uncaught_exceptions.
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|