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 testTryCatchFinally() { 17 let a = 1; 18 try { 19 a = 2; 20 } catch (e) { 21 print(e); 22 print(a); 23 a = 3; 24 } 25 print(a); 26 try { 27 a = 4; 28 } finally { 29 print(a); 30 a = 5; 31 } 32 print(a); 33 try { 34 a = 6; 35 } catch (e) { 36 print(e); 37 print(a); 38 a = 7; 39 } finally { 40 print(a); 41 a = 8; 42 } 43 print(a); 44} 45 46function testSimpleThrow() { 47 let a = 1; 48 print(a); 49 throw a; 50 a = 2; 51 print(a); 52} 53 54function testThrowInTry() { 55 let a = 1; 56 try { 57 a = 2; 58 throw a; 59 a = 3; 60 } catch (e) { 61 print(e); 62 print(a); 63 a = 4; 64 } 65 print(a); 66 try { 67 a = 5; 68 throw a; 69 a = 6; 70 } finally { 71 print(a); 72 a = 7; 73 } 74 print(a); 75 try { 76 a = 8; 77 throw a; 78 a = 9; 79 } catch (e) { 80 print(e); 81 print(a); 82 a = 10; 83 } finally { 84 print(a); 85 a = 11; 86 } 87 print(a); 88 a = 12; 89 print(a); 90} 91 92function testComplexThrow() { 93 let a = 1; 94 try { 95 try { 96 a = 2; 97 print(a); 98 throw a; 99 a = 3; 100 } catch (e) { 101 print(e); 102 print(a); 103 a = 4; 104 throw a; 105 a = 5; 106 } finally { 107 print(a); 108 a = 6; 109 throw a; 110 a = 7; 111 } 112 print(a); 113 a = 8; 114 } catch (e) { 115 print(e); 116 print(a); 117 a = 9; 118 } 119 print(a); 120} 121 122function testTryCatchFinallyWithReturn() { 123 let a = 1; 124 try { 125 a = 2; 126 print(a); 127 a = 3; 128 throw a; 129 a = 4; 130 print(a); 131 return a; 132 } catch (e) { 133 print(e); 134 print(a); 135 return; 136 a = 5; 137 } finally { 138 print(a); 139 a = 6; 140 print(a); 141 return a; 142 } 143 a = 7; 144 print(a); 145 return a; 146} 147 148testTryCatchFinally(); 149try { 150 testSimpleThrow(); 151} catch (e) { 152 print(e); 153} 154try { 155 testThrowInTry(); 156} catch (e) { 157 print(e); 158} 159try { 160 testComplexThrow(); 161} catch (e) { 162 print(e); 163} 164try { 165 print(testTryCatchFinallyWithReturn()); 166} catch (e) { 167 print(e); 168}