MISRA Discussion Forums
Rule 9.3.1 - iteration statement followed by 'try' - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=188)
+---- Forum: 4.9 Statements (https://forum.misra.org.uk/forumdisplay.php?fid=195)
+---- Thread: Rule 9.3.1 - iteration statement followed by 'try' (/showthread.php?tid=1734)



Rule 9.3.1 - iteration statement followed by 'try' - mstaron - 28-03-2025

It is possible to add the 'try' statement directly after 'while', for example:

class Exception{};
void f(int i)
{
    while(i) try {             // Non-compliant
        //...
    } catch(Exception e){
    }
}


I think this is non-compliant with the Rule 9.3.1, because 'try' is not a compound statement. However, accordingly to the standard 'try' should be always followed by a compound statement if it is inside a function. This code seems to be clear, so maybe this case could be added as an exception in the future versions of MISRA C++ standard.


RE: Rule 9.3.1 - iteration statement followed by 'try' - misra cpp - 28-03-2025

You are right, your example is non-compliant with 9.3.1.

Whilst 'try' is always followed by a compound statement, there are now a number of compound statements after the 'while', the 'try' and an arbitrary number of 'catch'es. For simplicity, we prefer 'while' followed by a single compound statement.