1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16function testNestedTryBlock() {
17    let a = 1;
18    try {
19        try {
20            throw a;
21            a = 2;
22        } catch (e) {
23            print(e)
24            print(a);
25            throw a;
26        }
27    } catch (e) {
28        print(e);
29        try {
30            a = 3;
31            throw a;
32            a = 4;
33        } finally {
34            print(a);
35            a = 5;
36        }
37    } finally {
38        print (a);
39        try {
40            a = 6;
41            throw a;
42            a = 7;
43        } catch (e) {
44            print(e);
45            print(a);
46            a = 8
47            throw a;
48            a = 9;
49        } finally {
50            print(a);
51            a = 10;
52        }
53    }
54}
55
56function testTryBlockWithLoop() {
57    let i = 0, j = 0, a = 1;
58    try {
59        outer: while (i < 10) {
60            i += 1;
61            try {
62                inner: while (j < 10) {
63                    j += 1;
64                    try {
65                        try {
66                            if (i === 3) {
67                                continue outer;
68                            }
69                            if (i === 5) {
70                                break outer;
71                            }
72                            if (j === 3) {
73                                continue;
74                            }
75                            if (j === 5) {
76                                break;
77                            }
78                            if (i === 7 && j === 5) {
79                                throw a;
80                            }
81                            if (i === 8 && j === 5) {
82                                return a;
83                            }
84                        } finally {
85                            print(a);
86                            a = 2;
87                        }
88                    } catch (e) {
89                        print(e);
90                        print(a);
91                        a = 3;
92                    } finally {
93                        print(a);
94                        a = 4;
95                    }
96                }
97            } finally {
98                print(a);
99                a = 6;
100            }
101        }
102    } finally {
103        print(a);
104        a = 7;
105    }
106}
107
108try {
109    testNestedTryBlock();
110} catch (e) {
111    print(e);
112}
113try {
114    testTryBlockWithLoop();
115} catch (e) {
116    print(e);
117}