summaryrefslogtreecommitdiff
path: root/ravenwood/runtime-common-src
diff options
context:
space:
mode:
author Makoto Onuki <omakoto@google.com> 2024-11-16 01:18:02 +0000
committer Gerrit Code Review <noreply-gerritcodereview@google.com> 2024-11-16 01:18:02 +0000
commit1ce0ccf1ac9217b52971ccaf24ab89d437beb465 (patch)
tree95b6dc10ecbc895fad7e8b9dc9be25ae5c2716de /ravenwood/runtime-common-src
parent2be55aa2ecd4d922ceee454b41d882e3c52bf409 (diff)
parent1e2f7e1d5a69c6dfab825e4530bad880984d99ad (diff)
Merge "Do not mask exceptions from initialization" into main
Diffstat (limited to 'ravenwood/runtime-common-src')
-rw-r--r--ravenwood/runtime-common-src/com/android/ravenwood/common/RavenwoodCommonUtils.java30
1 files changed, 29 insertions, 1 deletions
diff --git a/ravenwood/runtime-common-src/com/android/ravenwood/common/RavenwoodCommonUtils.java b/ravenwood/runtime-common-src/com/android/ravenwood/common/RavenwoodCommonUtils.java
index 520f050f0655..2bd079a9d135 100644
--- a/ravenwood/runtime-common-src/com/android/ravenwood/common/RavenwoodCommonUtils.java
+++ b/ravenwood/runtime-common-src/com/android/ravenwood/common/RavenwoodCommonUtils.java
@@ -30,6 +30,7 @@ import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
+import java.util.function.Supplier;
public class RavenwoodCommonUtils {
private static final String TAG = "RavenwoodCommonUtils";
@@ -277,8 +278,35 @@ public class RavenwoodCommonUtils {
(isStatic ? "static" : "")));
}
+ /**
+ * Run a supplier and swallow the exception, if any.
+ *
+ * It's a dangerous function. Only use it in an exception handler where we don't want to crash.
+ */
+ @Nullable
+ public static <T> T runIgnoringException(@NonNull Supplier<T> s) {
+ try {
+ return s.get();
+ } catch (Throwable th) {
+ log(TAG, "Warning: Exception detected! " + getStackTraceString(th));
+ }
+ return null;
+ }
+
+ /**
+ * Run a runnable and swallow the exception, if any.
+ *
+ * It's a dangerous function. Only use it in an exception handler where we don't want to crash.
+ */
+ public static void runIgnoringException(@NonNull Runnable r) {
+ runIgnoringException(() -> {
+ r.run();
+ return null;
+ });
+ }
+
@NonNull
- public static String getStackTraceString(@Nullable Throwable th) {
+ public static String getStackTraceString(@NonNull Throwable th) {
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter);
th.printStackTrace(writer);