rspec/rules/S1034/rule.adoc

43 lines
936 B
Plaintext
Raw Normal View History

== Why is this an issue?
The exception handling mechanism can provide an effective and clear means of handling error conditions, particularly where a function needs to return both some desired result together with an indication of success or failure. However, because of its ability to transfer control back up the call tree, it can also lead to code that is difficult to understand. Hence it is required that the mechanism is only used to capture behaviour that is in some sense undesirable, and which is not expected to be seen in normal program execution.
=== Noncompliant code example
2022-02-04 17:28:24 +01:00
[source,text]
----
void fn ( )
{
try
{
if ( x < 10 )
{
throw ( 10 );
}
// Action "A"
}
catch ( int32_t y ) // Noncompliant, confusing way of handling x < 10
{
// Action "B"
}
}
----
=== Compliant solution
2022-02-04 17:28:24 +01:00
[source,text]
----
if ( x < 10 )
{
// Action "B"
}
else
{
// Action "A"
}
----