AutosarC++19_03-A15.3.4

Catch-all (ellipsis and std::exception) handlers shall be used only in (a) main, (b) task main functions, (c) in functions that are supposed to isolate independent components and (d) when calling third-party code that uses exceptions not according to AUTOSAR C++14 guidelines

Required inputs: IR

Rationale

Catching an exception through catch-all handlers does not provide any detailed information about caught exception. This does not allow to take meaningful actions to recover from an exception other than to re-throw it. This is inefficient and results in code that is difficult to maintain.

Example
//% $Id: A15-3-4.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
    #include <stdexcept>
    #include <thread>
    extern std::int32_t Fn(); // Prototype of external third-party library function
    void F1() noexcept(false)
    {
        try
        {
            std::int32_t ret = Fn();
            // ...
        }

          // ...
          catch (...)   // Compliant
        {
               // Handle all unexpected exceptions from fn() function
        }
    }
    void F2() noexcept(false)
    {
        std::int32_t ret =
            Fn(); // Non-compliant - can not be sure whether fn() throws or not

        if (ret < 10)
        {
            throw std::underflow_error("Error");
        }

        else if (ret < 20)
        {
            // ...
        }
        else if (ret < 30)
        {
            throw std::overflow_error("Error");
        }

        else
        {
               throw std::range_error("Error");
        }
    }
    void F3() noexcept(false)
    {
        try
        {
            F2();
        }

        catch (std::exception& e)    // Non-compliant - caught exception is too
                                     // general, no information which error occured
        {
               // Nothing to do
               throw;
        }
    }
    void F4() noexcept(false)
    {
        try
        {
            F3();
        }

        catch (...)    // Non-compliant - no information about the exception
        {
             // Nothing to do
             throw;
        }
    }
    class ExecutionManager
    {
      public:
        ExecutionManager() = default;
        void Execute() noexcept(false)
        {
            try
            {
                F3();
            }

             // ...
             catch (std::exception& e) // Compliant
             {
                 // Handle all expected exceptions
             }
             catch (...) // Compliant
             {
                 // Handle all unexpected exceptions
             }
        }
    };
    void ThreadMain() noexcept
    {
        try
        {
            F3();
        }

        // ...
         catch (std::exception& e) // Compliant
         {
             // Handle all expected exceptions
         }
         catch (...) // Compliant
         {
             // Handle all unexpected exceptions
         }
     }
     int main(int, char**)
     {
         try
         {
             ExecutionManager execManager;
             execManager.Execute();
             // ...
             std::thread t(&ThreadMain);
               // ...
               F4();
           }

           // ...
           catch (std::exception& e) // Compliant
           {
               // Handle all expected exceptions
           }
           catch (...) // Compliant
           {
               // Handle all unexpected exceptions
           }

           return 0;
     }
See Also
  • none

Possible Messages

Key

Text

Severity

Disabled

catch_all

Use of catch(…).

None

False

catch_std_exception

Catching std::exceptions is too general.

None

False

Options

allow_in_extern_c_functions

allow_in_extern_c_functions : bool = False

Whether to allow a catch-all in extern C functions.
 

allow_in_functions_matching

allow_in_functions_matching : typing.Pattern[str] | None = None

If not None, a re.compile()ed object where functions whose qualified name matches the regex are allowed to contain catch-alls.
 

allow_in_main

allow_in_main : bool = True

Whether to allow a catch-all in the main() function.
 

allow_in_thread_main

allow_in_thread_main : bool = True

Whether to allow a catch-all in thread-main functions.
 

allow_rethrow

allow_rethrow : bool = False

Whether to allow a catch-all with a re-throw.
 

autosar_exception_model

autosar_exception_model

Type: bauhaus.ir.autosar.exceptions.autosar_exceptions.AutosarExceptionModel

Default: <bauhaus.ir.autosar.exceptions.autosar_exceptions.AutosarExceptionModel object at 0x7f3bf46fd820>

The default model for what counts as an checked / unchecked exception.
 

disallow_std_exception

disallow_std_exception : bool = True

Whether to consider catching the literal std::exception as a catch-all.