public static byte[] serializeOrDiscard(StateObject stateObject) throws Exception {
        try {
            return InstantiationUtil.serializeObject(stateObject);
        } catch (Exception e) {
            try {
                stateObject.discardState();
            } catch (Exception discardException) {
                e.addSuppressed(discardException);
            }

            ExceptionUtils.rethrowException(e);
        }

        // will never happen but is added to please the compiler
        return new byte[0];
    }
@Test
    void testSerializeOrDiscardFailureHandling() throws Exception {
        final AtomicBoolean discardCalled = new AtomicBoolean(false);
        final StateObject original =
                new FailingSerializationStateObject(() -> discardCalled.set(true));

        assertThatThrownBy(() -> StateHandleStoreUtils.serializeOrDiscard(original))
                .withFailMessage("An IOException is expected to be thrown.")
                .isInstanceOf(IOException.class);

        assertThat(discardCalled).isTrue();
    }