mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-19 05:30:43 +00:00
Add tryIgnore, tryCatch methods
This commit is contained in:
@ -26,6 +26,8 @@ import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
**
|
||||
***************************************************************************/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,6 +22,7 @@
|
||||
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.exceptions.QException;
|
||||
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.assertNull;
|
||||
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.
|
||||
*******************************************************************************/
|
||||
|
Reference in New Issue
Block a user