michael@0: // Copyright 2009 the Sputnik authors. All rights reserved. michael@0: // This code is governed by the BSD license found in the LICENSE file. michael@0: michael@0: /** michael@0: * Using "try" with "catch" or "finally" statement within/without a "switch" statement michael@0: * michael@0: * @path ch12/12.14/S12.14_A15.js michael@0: * @description Insert try/catch/finally to switch statement michael@0: */ michael@0: michael@0: // CHECK#1 michael@0: function SwitchTest1(value){ michael@0: var result = 0; michael@0: try{ michael@0: switch(value) { michael@0: case 1: michael@0: result += 4; michael@0: throw result; michael@0: break; michael@0: default: michael@0: result += 32; michael@0: break; michael@0: case 4: michael@0: result += 64; michael@0: throw "ex"; michael@0: } michael@0: return result; michael@0: } michael@0: catch(e){ michael@0: if ((value===1)&&(e!==4)) $ERROR('#1.1: Exception ===4. Actual: Exception ==='+ e ); michael@0: if ((value===4)&&(e!=="ex")) $ERROR('#1.2: Exception ==="ex". Actual: Exception ==='+ e ); michael@0: } michael@0: finally{ michael@0: return result; michael@0: } michael@0: } michael@0: if (SwitchTest1(1)!==4) $ERROR('#1.3: SwitchTest1(1)===4. Actual: SwitchTest1(1)==='+ SwitchTest1(1) ); michael@0: if (SwitchTest1(4)!==64) $ERROR('#1.4: SwitchTest1(4)===64. Actual: SwitchTest1(4)==='+ SwitchTest1(4) ); michael@0: michael@0: // CHECK#2 michael@0: var c2=0; michael@0: function SwitchTest2(value){ michael@0: var result = 0; michael@0: switch(value) { michael@0: case 0: michael@0: try{ michael@0: result += 2; michael@0: break; michael@0: } michael@0: finally{ michael@0: c2=1; michael@0: } michael@0: case 1: michael@0: result += 4; michael@0: break; michael@0: default: michael@0: result += 32; michael@0: break; michael@0: } michael@0: return result; michael@0: } michael@0: if (SwitchTest2(1)!==4) $ERROR('#2.1: SwitchTest1(1)===4. Actual: SwitchTest1(1)==='+ SwitchTest1(1) ); michael@0: if (c2===1) $ERROR('#2.2: Evaluate finally block'); michael@0: if (SwitchTest2(0)!==2) $ERROR('#2.3: SwitchTest1(0)===2. Actual: SwitchTest1(0)==='+ SwitchTest1(0) ); michael@0: if (c2!==1) $ERROR('#2.4: "finally" block must be evaluated'); michael@0: michael@0: // CHECK#3 michael@0: function SwitchTest3(value){ michael@0: var result = 0; michael@0: switch(value) { michael@0: case 0: michael@0: try{ michael@0: result += 2; michael@0: throw "ex"; michael@0: } michael@0: finally{ michael@0: break; michael@0: } michael@0: default: michael@0: result += 32; michael@0: break; michael@0: } michael@0: return result; michael@0: } michael@0: try{ michael@0: var x3=SwitchTest3(0); michael@0: if (x3!==2) $ERROR('#3.1: x3===2. Actual: x3==='+x3); michael@0: } michael@0: catch(e){ michael@0: $ERROR('#3.2: Catching exception inside function does not lead to throwing exception outside this function'); michael@0: } michael@0: