]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
1 | /** |
2 | * File Name: try-001.js | |
3 | * ECMA Section: | |
4 | * Description: The try statement | |
5 | * | |
6 | * This test contains try, catch, and finally blocks. An exception is | |
7 | * sometimes thrown by a function called from within the try block. | |
8 | * | |
9 | * This test doesn't actually make any LiveConnect calls. | |
10 | * | |
11 | * | |
12 | * Author: christine@netscape.com | |
13 | * Date: 11 August 1998 | |
14 | */ | |
15 | var SECTION = ""; | |
16 | var VERSION = "ECMA_2"; | |
17 | var TITLE = "The try statement"; | |
18 | ||
19 | startTest(); | |
20 | writeHeaderToLog( SECTION + " "+ TITLE); | |
21 | ||
22 | var tc = 0; | |
23 | var testcases = new Array(); | |
24 | ||
25 | var INVALID_JAVA_INTEGER_VALUE = "Invalid value for java.lang.Integer constructor"; | |
26 | ||
27 | TryNewJavaInteger( "3.14159", INVALID_JAVA_INTEGER_VALUE ); | |
28 | TryNewJavaInteger( NaN, INVALID_JAVA_INTEGER_VALUE ); | |
29 | TryNewJavaInteger( 0, 0 ); | |
30 | TryNewJavaInteger( -1, -1 ); | |
31 | TryNewJavaInteger( 1, 1 ); | |
32 | TryNewJavaInteger( Infinity, Infinity ); | |
33 | ||
34 | test(); | |
35 | ||
36 | /** | |
37 | * Check to see if the input is valid for java.lang.Integer. If it is | |
38 | * not valid, throw INVALID_JAVA_INTEGER_VALUE. If input is valid, | |
39 | * return Number( v ) | |
40 | * | |
41 | */ | |
42 | ||
43 | function newJavaInteger( v ) { | |
44 | value = Number( v ); | |
45 | if ( Math.floor(value) != value || isNaN(value) ) { | |
46 | throw ( INVALID_JAVA_INTEGER_VALUE ); | |
47 | } else { | |
48 | return value; | |
49 | } | |
50 | } | |
51 | ||
52 | /** | |
53 | * Call newJavaInteger( value ) from within a try block. Catch any | |
54 | * exception, and store it in result. Verify that we got the right | |
55 | * return value from newJavaInteger in cases in which we do not expect | |
56 | * exceptions, and that we got the exception in cases where an exception | |
57 | * was expected. | |
58 | */ | |
59 | function TryNewJavaInteger( value, expect ) { | |
60 | var finalTest = false; | |
61 | ||
62 | try { | |
63 | result = newJavaInteger( value ); | |
64 | } catch ( e ) { | |
65 | result = String( e ); | |
66 | } finally { | |
67 | finalTest = true; | |
68 | } | |
69 | testcases[tc++] = new TestCase( | |
70 | SECTION, | |
71 | "newJavaValue( " + value +" )", | |
72 | expect, | |
73 | result); | |
74 | ||
75 | testcases[tc++] = new TestCase( | |
76 | SECTION, | |
77 | "newJavaValue( " + value +" ) hit finally block", | |
78 | true, | |
79 | finalTest); | |
80 | ||
81 | } | |
82 |