michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: * File Name: try-006.js michael@0: * ECMA Section: michael@0: * Description: The try statement michael@0: * michael@0: * Throw an exception from within a With block in a try block. Verify michael@0: * that any expected exceptions are caught. michael@0: * michael@0: * Author: christine@netscape.com michael@0: * Date: 11 August 1998 michael@0: */ michael@0: var SECTION = "try-006"; michael@0: var VERSION = "ECMA_2"; michael@0: var TITLE = "The try statement"; michael@0: michael@0: startTest(); michael@0: writeHeaderToLog( SECTION + " "+ TITLE); michael@0: michael@0: /** michael@0: * This is the "check" function for test objects that will michael@0: * throw an exception. michael@0: */ michael@0: function throwException() { michael@0: throw EXCEPTION_STRING +": " + this.valueOf(); michael@0: } michael@0: var EXCEPTION_STRING = "Exception thrown:"; michael@0: michael@0: /** michael@0: * This is the "check" function for test objects that do not michael@0: * throw an exception michael@0: */ michael@0: function noException() { michael@0: return this.valueOf(); michael@0: } michael@0: michael@0: /** michael@0: * Add test cases here michael@0: */ michael@0: TryWith( new TryObject( "hello", throwException, true )); michael@0: TryWith( new TryObject( "hola", noException, false )); michael@0: michael@0: /** michael@0: * Run the test. michael@0: */ michael@0: michael@0: test(); michael@0: michael@0: /** michael@0: * This is the object that will be the "this" in a with block. michael@0: */ michael@0: function TryObject( value, fun, exception ) { michael@0: this.value = value; michael@0: this.exception = exception; michael@0: michael@0: this.valueOf = new Function ( "return this.value" ); michael@0: this.check = fun; michael@0: } michael@0: michael@0: /** michael@0: * This function has the try block that has a with block within it. michael@0: * Test cases are added in this function. Within the with block, the michael@0: * object's "check" function is called. If the test object's exception michael@0: * property is true, we expect the result to be the exception value. michael@0: * If exception is false, then we expect the result to be the value of michael@0: * the object. michael@0: */ michael@0: function TryWith( object ) { michael@0: try { michael@0: with ( object ) { michael@0: result = check(); michael@0: } michael@0: } catch ( e ) { michael@0: result = e; michael@0: } michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "TryWith( " + object.value +" )", michael@0: (object.exception ? EXCEPTION_STRING +": " + object.valueOf() : object.valueOf()), michael@0: result ); michael@0: }