Add tryIgnore, tryCatch methods

This commit is contained in:
2025-05-27 16:44:45 -05:00
parent d63f13bb55
commit 437448fd81
2 changed files with 65 additions and 0 deletions

View File

@ -26,6 +26,8 @@ import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer;
import com.kingsrook.qqq.backend.core.utils.lambdas.UnsafeVoidVoidMethod;
/******************************************************************************* /*******************************************************************************
@ -183,4 +185,41 @@ public class ExceptionUtils
} }
return (rs); return (rs);
} }
/***************************************************************************
**
***************************************************************************/
public static void tryIgnore(UnsafeVoidVoidMethod<?> method)
{
try
{
method.run();
}
catch(Throwable t)
{
///////////////////////////
// ignore, as name says! //
///////////////////////////
}
}
/***************************************************************************
**
***************************************************************************/
public static void tryCatch(UnsafeVoidVoidMethod<?> method, Consumer<Throwable> catcher)
{
try
{
method.run();
}
catch(Throwable t)
{
catcher.accept(t);
}
}
} }

View File

@ -22,6 +22,7 @@
package com.kingsrook.qqq.backend.core.utils; package com.kingsrook.qqq.backend.core.utils;
import java.util.concurrent.atomic.AtomicBoolean;
import com.kingsrook.qqq.backend.core.BaseTest; import com.kingsrook.qqq.backend.core.BaseTest;
import com.kingsrook.qqq.backend.core.exceptions.QException; import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.exceptions.QUserFacingException; import com.kingsrook.qqq.backend.core.exceptions.QUserFacingException;
@ -29,6 +30,7 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
/******************************************************************************* /*******************************************************************************
@ -129,6 +131,30 @@ class ExceptionUtilsTest extends BaseTest
/*******************************************************************************
**
*******************************************************************************/
@Test
void testTryIgnore()
{
ExceptionUtils.tryIgnore(() -> ((Integer) null).toString());
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testTryCatch()
{
AtomicBoolean didCatch = new AtomicBoolean(false);
ExceptionUtils.tryCatch(() -> ((Integer) null).toString(), e -> didCatch.set(true));
assertTrue(didCatch.get());
}
/******************************************************************************* /*******************************************************************************
** Test exception class - lets you set the cause, easier to create a loop. ** Test exception class - lets you set the cause, easier to create a loop.
*******************************************************************************/ *******************************************************************************/