This issue is discussed also in AN5342.
Newer STM32 families feature error-check-and-correction (ECC) hardware on their FLASH, and even newer ones also on (portions of) SRAM (and even Cache). While ECC unit(s) can detect and correct a single-bit error within the FLASH/SRAM row (extended by the ECC "checksum"), it can't cope with double-bit errors (or more). Reading such row then leads generally to NMI, as that's an unrecoverable hardware error which has to be coped with using some exception mechanism.
To prevent such situation to happen, first, the mechanism of failure has to be understood.
For FLASH, there are two mechanisms which can come into consideration: charge leakage and corruption due to power removal during programming/erasing.
If FLASH is used only for the code and fixed data, where these are normally written only once or a very few times, charge leakage is not significant and FLASH is guaranteed to provide correct data up until the retention parameter given by datasheet, usually between 10 to 100 years, often 20 years. In other words, in most applications, this mechanism of failure is improbable and does not need to be considered.
In applications where the mcu/FLASH is operated at elevated temperatures, the FLASH retention may be shortened significantly, and in such applications measures like periodic FLASH check and renewal may be employed; but such techniques are beyond the scope of usual applications and will need a thorough cost-benefit analysis before being utilized.
Power removal 1 during programming/erasing is the dominant mechanism for FLASH corruption thus ECC errors in applications, where portions of FLASH are used to store changing data, in EEPROM-like fashion. It can also happen in applications where firmware update is performed using some sort of bootloader. In these cases, if it is necessary to prevent the corruption, it has to be ensured that erasing/programmig is not be started if the power is being removed, and that there is enough power reserve in local storage to cover the total consumption during thelongest expected programming time (both based on data found in datasheets). In other words, before perasing/programming is started, the program has to check whether power source hasn't been removed e.g. using a comparator (in same cases the PVD detector built into some of STM32 families can be used for this purpose); and VDD has to be decoupled by bulk capacitance which retains adequate voltage for the total erasing/programming time after power removal has been detected.
Another method may involve accepting the possibility of corruption, and doubling or tripling the data, while also implementing methods to find out which copy is consistent Besides properly handling the ECC errors, it means also using versioning/timestamping, using CRC or other forms of checksumming, etc. 2
For SRAM, the failure mechanism is somewhat different. SRAM write is almost instantaneous, thus there is no risk of inadequate SRAM write while power is removed (relevant only for battery-backuped SRAM). So, we here have either a read from uninitialized SRAM portions, or SRAM bit corrupted (flipped) by impact of ionizing radiation such as alpha particles (this is equivalent to the FLASH leakage; SRAM may leak, too, but that's practically excluded in properly designed, manufactured and tested parts).
Reads from uninitialized SRAM are to be mitigated by proper programming techniques: explicitly initializing all used SRAM (in STM32 models which employ caches, this includes SRAM portions which are not used by the program explicitly, but could be read during cache line fills); and avoiding errors such as dereferencing runaway pointers. The latter is of course easier said than done, so it may be wise to expect such errors when writing the ECC interrupt handlers, too.
Ionizing radiation is unlikely to cause more than a single SRAM bit flip at a time, and single-bit-error ECC is corrected by the hardware automatically. However, if these are ignored - either because there is no single-bit-error ECC handling, or simply because a given memory address is read only very rarely - and the SRAM is up for a long time, another similar bit flip may occur in the same SRAM row, causing a non-recoverable double-bit-error 3. So, the proper strategy here is to have a mechanism which periodically (with periods in hours or days) reads out all the SRAM content, and if a single-bit-error ECC even is detected, write back the (ECC-corrected so still valid) data (in fact, it should be enough to write back the data unconditionally, in a way which does not interfere with the application). The rate of occurence of single-bit ECC event then could be used to determine, whether the "refresh" period is adequate, and estimate what's the probability of double-bit error.
1. It appears that at least in some STM32 system reset aborts FLASH erase/programming, leading to FLASH corruption, too. Thus in applications, where an external reset may occur without power removal, or watchdog is used, these has to be taken into mind. Either the mechanisms leading to reset should be designed so that they provide a early-warning-interrupt which then allows to finish the FLASH erasing/programming (e.g. using WWDG with EWI); or the occurence of FLASH corruption has to be accepted using the mechanisms outlined above.
2. One technique is to check FLASH for ECC errors in bootloader, before starting the application. While the ECC errors can be handled in the "usual" interrupts, it may be simpler and more convenient simply to perform a scan (perhaps also calculating CRC, perhaps utilizing DMA, etc.) with no interrupt, and only after that check, whether an ECC error occured. NMI - as it's name suggests - is non-maskable (although, somewhat weirdly, e.g. in 'H5 the double-bit ECC trigger to NMI can be masked by SBS_ECCNMIR.ECCNMI_MASK_EN). User MrJorge posted an interesting technique (although certainly not a universally applicable one), where the NMI is suppressed by setting both FAULTMASK = 1 (resulting in execution level -1, suppressing all interrupts and also some of the exceptions), and the SCB_CCR.BFHFNMIGN bit, which results in ignoring bus faults.
3. An anecdotal estimate of rate of presumably double-bit-error in SRAM - with no details as per the STM32 family, SRAM size, programming techniques employed, etc. - is given in this post: For SRAM we had a case of small gas meter manufacturer that manufactured ~500k meters neglecting the safety standards, had ~100 returns the first year only, all of them due to SRAM ECC failure.