1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.app.appsearch.util;
18 
19 import android.annotation.NonNull;
20 import android.os.RemoteException;
21 
22 /**
23  * Utilities for handling exceptions.
24  *
25  * @hide
26  */
27 public final class ExceptionUtil {
28 
29     /**
30      * {@link RuntimeException} will be rethrown if {@link #isItOkayToRethrowException()} returns
31      * true.
32      */
handleException(@onNull Exception e)33     public static void handleException(@NonNull Exception e) {
34         if (isItOkayToRethrowException() && e instanceof RuntimeException) {
35             rethrowRuntimeException((RuntimeException) e);
36         }
37     }
38 
39     /** Returns whether it is OK to rethrow exceptions from this entrypoint. */
isItOkayToRethrowException()40     private static boolean isItOkayToRethrowException() {
41         return false;
42     }
43 
44     /** Rethrow exception from SystemServer in Framework code. */
handleRemoteException(@onNull RemoteException e)45     public static void handleRemoteException(@NonNull RemoteException e) {
46         e.rethrowFromSystemServer();
47     }
48 
49     /**
50      * A helper method to rethrow {@link RuntimeException}.
51      *
52      * <p>We use this to enforce exception type and assure the compiler/linter that the exception is
53      * indeed {@link RuntimeException} and can be rethrown safely.
54      */
rethrowRuntimeException(RuntimeException e)55     private static void rethrowRuntimeException(RuntimeException e) {
56         throw e;
57     }
58 
ExceptionUtil()59     private ExceptionUtil() {}
60 }
61