|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 * File Name: try-001.js |
|
9 * ECMA Section: |
|
10 * Description: The try statement |
|
11 * |
|
12 * This test contains try, catch, and finally blocks. An exception is |
|
13 * sometimes thrown by a function called from within the try block. |
|
14 * |
|
15 * |
|
16 * Author: christine@netscape.com |
|
17 * Date: 11 August 1998 |
|
18 */ |
|
19 var SECTION = ""; |
|
20 var VERSION = "ECMA_2"; |
|
21 var TITLE = "The try statement"; |
|
22 |
|
23 startTest(); |
|
24 writeHeaderToLog( SECTION + " "+ TITLE); |
|
25 |
|
26 var INVALID_JAVA_INTEGER_VALUE = "Invalid value for java.lang.Integer constructor"; |
|
27 |
|
28 TryNewJavaInteger( "3.14159", INVALID_JAVA_INTEGER_VALUE ); |
|
29 TryNewJavaInteger( NaN, INVALID_JAVA_INTEGER_VALUE ); |
|
30 TryNewJavaInteger( 0, 0 ); |
|
31 TryNewJavaInteger( -1, -1 ); |
|
32 TryNewJavaInteger( 1, 1 ); |
|
33 TryNewJavaInteger( Infinity, Infinity ); |
|
34 |
|
35 test(); |
|
36 |
|
37 /** |
|
38 * Check to see if the input is valid for java.lang.Integer. If it is |
|
39 * not valid, throw INVALID_JAVA_INTEGER_VALUE. If input is valid, |
|
40 * return Number( v ) |
|
41 * |
|
42 */ |
|
43 |
|
44 function newJavaInteger( v ) { |
|
45 value = Number( v ); |
|
46 if ( Math.floor(value) != value || isNaN(value) ) { |
|
47 throw ( INVALID_JAVA_INTEGER_VALUE ); |
|
48 } else { |
|
49 return value; |
|
50 } |
|
51 } |
|
52 |
|
53 /** |
|
54 * Call newJavaInteger( value ) from within a try block. Catch any |
|
55 * exception, and store it in result. Verify that we got the right |
|
56 * return value from newJavaInteger in cases in which we do not expect |
|
57 * exceptions, and that we got the exception in cases where an exception |
|
58 * was expected. |
|
59 */ |
|
60 function TryNewJavaInteger( value, expect ) { |
|
61 var finalTest = false; |
|
62 |
|
63 try { |
|
64 result = newJavaInteger( value ); |
|
65 } catch ( e ) { |
|
66 result = String( e ); |
|
67 } finally { |
|
68 finalTest = true; |
|
69 } |
|
70 new TestCase( |
|
71 SECTION, |
|
72 "newJavaValue( " + value +" )", |
|
73 expect, |
|
74 result); |
|
75 |
|
76 new TestCase( |
|
77 SECTION, |
|
78 "newJavaValue( " + value +" ) hit finally block", |
|
79 true, |
|
80 finalTest); |
|
81 |
|
82 } |
|
83 |