sonar-delphi
sonar-delphi copied to clipboard
New rule: Heap exceptions should not be created
Prerequisites
- [X] This rule has not already been suggested.
- [X] This should be a new rule, not an improvement to an existing rule.
- [X] This rule would be generally useful, not specific to my code or setup.
Suggested rule title
Heap exceptions should not be created
Rule description
This rule would detect constructor calls of all types inheriting from EHeapException (EOutOfMemory and EInvalidPointer).
Rationale
Exceptions descending from EHeapException have their memory pre-allocated at the start of the application. Creating them at runtime is at best misleading and at worst has unpredictable consequences.
See the Embarcadero documentation:
Note: Memory for these exceptions is pre-allocated whenever an application starts and remains allocated as long as the application is running. Never raise EHeapException or its descendants directly.
This one is a no-brainer, let's do it.