|
1 // Copyright 2009 the Sputnik authors. All rights reserved. |
|
2 // This code is governed by the BSD license found in the LICENSE file. |
|
3 |
|
4 /** |
|
5 * Using "try" with "catch" or "finally" statement within/without a "switch" statement |
|
6 * |
|
7 * @path ch12/12.14/S12.14_A15.js |
|
8 * @description Insert try/catch/finally to switch statement |
|
9 */ |
|
10 |
|
11 // CHECK#1 |
|
12 function SwitchTest1(value){ |
|
13 var result = 0; |
|
14 try{ |
|
15 switch(value) { |
|
16 case 1: |
|
17 result += 4; |
|
18 throw result; |
|
19 break; |
|
20 default: |
|
21 result += 32; |
|
22 break; |
|
23 case 4: |
|
24 result += 64; |
|
25 throw "ex"; |
|
26 } |
|
27 return result; |
|
28 } |
|
29 catch(e){ |
|
30 if ((value===1)&&(e!==4)) $ERROR('#1.1: Exception ===4. Actual: Exception ==='+ e ); |
|
31 if ((value===4)&&(e!=="ex")) $ERROR('#1.2: Exception ==="ex". Actual: Exception ==='+ e ); |
|
32 } |
|
33 finally{ |
|
34 return result; |
|
35 } |
|
36 } |
|
37 if (SwitchTest1(1)!==4) $ERROR('#1.3: SwitchTest1(1)===4. Actual: SwitchTest1(1)==='+ SwitchTest1(1) ); |
|
38 if (SwitchTest1(4)!==64) $ERROR('#1.4: SwitchTest1(4)===64. Actual: SwitchTest1(4)==='+ SwitchTest1(4) ); |
|
39 |
|
40 // CHECK#2 |
|
41 var c2=0; |
|
42 function SwitchTest2(value){ |
|
43 var result = 0; |
|
44 switch(value) { |
|
45 case 0: |
|
46 try{ |
|
47 result += 2; |
|
48 break; |
|
49 } |
|
50 finally{ |
|
51 c2=1; |
|
52 } |
|
53 case 1: |
|
54 result += 4; |
|
55 break; |
|
56 default: |
|
57 result += 32; |
|
58 break; |
|
59 } |
|
60 return result; |
|
61 } |
|
62 if (SwitchTest2(1)!==4) $ERROR('#2.1: SwitchTest1(1)===4. Actual: SwitchTest1(1)==='+ SwitchTest1(1) ); |
|
63 if (c2===1) $ERROR('#2.2: Evaluate finally block'); |
|
64 if (SwitchTest2(0)!==2) $ERROR('#2.3: SwitchTest1(0)===2. Actual: SwitchTest1(0)==='+ SwitchTest1(0) ); |
|
65 if (c2!==1) $ERROR('#2.4: "finally" block must be evaluated'); |
|
66 |
|
67 // CHECK#3 |
|
68 function SwitchTest3(value){ |
|
69 var result = 0; |
|
70 switch(value) { |
|
71 case 0: |
|
72 try{ |
|
73 result += 2; |
|
74 throw "ex"; |
|
75 } |
|
76 finally{ |
|
77 break; |
|
78 } |
|
79 default: |
|
80 result += 32; |
|
81 break; |
|
82 } |
|
83 return result; |
|
84 } |
|
85 try{ |
|
86 var x3=SwitchTest3(0); |
|
87 if (x3!==2) $ERROR('#3.1: x3===2. Actual: x3==='+x3); |
|
88 } |
|
89 catch(e){ |
|
90 $ERROR('#3.2: Catching exception inside function does not lead to throwing exception outside this function'); |
|
91 } |
|
92 |