xref: /aosp_15_r20/art/test/2282-checker-always-throws-try-catch/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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 public class Main {
main(String[] args)18     public static void main(String[] args) {
19         $noinline$testAlwaysThrows();
20     }
21 
22     // This never visibily throws as all throws are caught within the same method.
alwaysThrowsInfiniteLoop()23     private static void alwaysThrowsInfiniteLoop() {
24         while (true) {
25             try {
26                 throw new Error();
27             } catch (Error expected) {
28             }
29         }
30     }
31 
alwaysThrows()32     private static void alwaysThrows() throws Exception {
33         try {
34             throw new Error();
35         } catch (Error expected) {
36             throw new Exception();
37         }
38     }
39 
40     /// CHECK-START: void Main.$noinline$testAlwaysThrows() inliner (after)
41     /// CHECK: InvokeStaticOrDirect method_name:Main.alwaysThrows always_throws:false
$noinline$testAlwaysThrows()42     private static void $noinline$testAlwaysThrows() {
43         try {
44             alwaysThrows();
45             System.out.println("alwaysThrows didn't throw");
46         } catch (Exception expected) {
47         }
48     }
49 
50     // Don't call this method as it has an infinite loop
51 
52     /// CHECK-START: void Main.$noinline$doNotCallInfiniteLoop() inliner (after)
53     /// CHECK: InvokeStaticOrDirect method_name:Main.alwaysThrowsInfiniteLoop always_throws:false
$noinline$doNotCallInfiniteLoop()54     private static void $noinline$doNotCallInfiniteLoop() {
55         alwaysThrowsInfiniteLoop();
56     }
57 }
58