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-001.js michael@0: * ECMA Section: michael@0: * Description: The try statement michael@0: * michael@0: * This test contains try, catch, and finally blocks. An exception is michael@0: * sometimes thrown by a function called from within the try block. michael@0: * michael@0: * michael@0: * Author: christine@netscape.com michael@0: * Date: 11 August 1998 michael@0: */ michael@0: var SECTION = ""; 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: var INVALID_JAVA_INTEGER_VALUE = "Invalid value for java.lang.Integer constructor"; michael@0: michael@0: TryNewJavaInteger( "3.14159", INVALID_JAVA_INTEGER_VALUE ); michael@0: TryNewJavaInteger( NaN, INVALID_JAVA_INTEGER_VALUE ); michael@0: TryNewJavaInteger( 0, 0 ); michael@0: TryNewJavaInteger( -1, -1 ); michael@0: TryNewJavaInteger( 1, 1 ); michael@0: TryNewJavaInteger( Infinity, Infinity ); michael@0: michael@0: test(); michael@0: michael@0: /** michael@0: * Check to see if the input is valid for java.lang.Integer. If it is michael@0: * not valid, throw INVALID_JAVA_INTEGER_VALUE. If input is valid, michael@0: * return Number( v ) michael@0: * michael@0: */ michael@0: michael@0: function newJavaInteger( v ) { michael@0: value = Number( v ); michael@0: if ( Math.floor(value) != value || isNaN(value) ) { michael@0: throw ( INVALID_JAVA_INTEGER_VALUE ); michael@0: } else { michael@0: return value; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Call newJavaInteger( value ) from within a try block. Catch any michael@0: * exception, and store it in result. Verify that we got the right michael@0: * return value from newJavaInteger in cases in which we do not expect michael@0: * exceptions, and that we got the exception in cases where an exception michael@0: * was expected. michael@0: */ michael@0: function TryNewJavaInteger( value, expect ) { michael@0: var finalTest = false; michael@0: michael@0: try { michael@0: result = newJavaInteger( value ); michael@0: } catch ( e ) { michael@0: result = String( e ); michael@0: } finally { michael@0: finalTest = true; michael@0: } michael@0: new TestCase( michael@0: SECTION, michael@0: "newJavaValue( " + value +" )", michael@0: expect, michael@0: result); michael@0: michael@0: new TestCase( michael@0: SECTION, michael@0: "newJavaValue( " + value +" ) hit finally block", michael@0: true, michael@0: finalTest); michael@0: michael@0: } michael@0: