Rule A15-3-2 (required, implementation, non-automated)
If a function throws an exception, it shall be handled when meaningful actions can be taken, otherwise it shall be propagated.
Rationale
Provide exception handlers only for functions that actually are able to take recovery or cleanup actions. Implementing meaningless exception handlers that only re-throw caught exception results in an implementation that is inefficient and difficult to maintain.
Example
//% $Id: A15-3-2.cpp 309502 2018-02-28 09:17:39Z michal.szczepankiewicz $
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <memory>
/// @checkedException
class CommunicationError : public std::exception
{
// Implementation
};
/// @throw CommunicationError Exceptional communication errors
extern void Send(std::uint8_t* buffer) noexcept(false);
void SendData1(std::uint8_t* data) noexcept(false)
{
try
{
Send(data);
}
catch (CommunicationError& e)
{
std::cerr << "Communication error occured" << std::endl;
throw; // Non-compliant - exception is not handled, just re-thrown
}
}
extern void BusRestart() noexcept;
extern void BufferClean() noexcept;
void SendData2(std::uint8_t* data) noexcept(false)
{
try
{
Send(data);
}
catch (CommunicationError& e)
{
std::cerr << "Communication error occured" << std::endl;
BufferClean();
throw; // Compliant - exception is partially handled and re-thrown
}
}
void F1() noexcept
{
std::uint8_t* buffer = nullptr;
// ...
try
{
SendData2(buffer);
}
catch (CommunicationError& e)
{
std::cerr << "Communication error occured" << std::endl;
BusRestart();
// Compliant - including SendData2() exception handler, exception is now
// fully handled
}
}
void SendData3(std::uint8_t* data) noexcept
{
try
{
Send(data);
}
catch (CommunicationError& e)
{
std::cerr << "Communication error occured" << std::endl;
BufferClean();
BusRestart();
// Compliant - exception is fully handled
}
}
struct A
{
std::uint32_t x;
};
std::unique_ptr<A[]> Func1()
{
//rather throws std::bad_alloc
return std::make_unique<A[]>(999999999999999999);
}
std::unique_ptr<A[]> Func2()
{
//does not catch std::bad_alloc
//because nothing meaningful can be done here
return Func1();
}
std::unique_ptr<A[]> Func3()
{
//does not catch std::bad_alloc
//because nothing meaningful can be done here
return Func2();
}
extern void Cleanup() noexcept;
int main(void)
{
try
{
Func3();
}
catch (const std::exception& ex)
{
//catches std::bad_alloc here and
//terminates the application
//gracefully
Cleanup();
}
return 0;
}
See also
none