I have went back and I am getting an old modpack of mine to work with 1.21.4, which I named "the 128mb pack". The sole purpose of it is to make the game run on 128MB of allocated memory (which it does), but it also constantly runs out of memory if you do something it doesn't like. When it runs out of memory though, usually the game completely freezes.
The premise of catching this is to wait for an OutOfMemoryException, and then activate.
My idea with this is a mod that shows you an out of memory screen with a button to quit the game, so instead of it freezing you can just quit the game.
If this implementation uses too much memory, having it just instantaneously quit is also fine too.