errors.js revision 877:cf4d2252d444
1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * Basic checks for Error constructors.
26 *
27 * @test
28 * @run
29 */
30
31print(Error.name + " is a " + typeof(Error));
32print(EvalError.name + " is a " + typeof(EvalError));
33print(RangeError.name + " is a " + typeof(RangeError));
34print(ReferenceError.name + " is a " + typeof(ReferenceError));
35print(SyntaxError.name + " is a " + typeof(SyntaxError));
36print(TypeError.name + " is a " + typeof(TypeError));
37print(URIError.name + " is a " + typeof(URIError));
38
39print("Error.arity " + Error.length);
40print("EvalError.arity " + EvalError.length);
41print("RangeError.arity " + RangeError.length);
42print("ReferenceError.arity " + ReferenceError.length);
43print("SyntaxError.arity " + SyntaxError.length);
44print("TypeError.arity " + TypeError.length);
45print("URIError.arity " + URIError.length);
46
47var err = new Error("my error");
48try {
49    throw err;
50} catch (e) {
51    print(e instanceof Error);
52    print(e.message);
53    print(e.name);
54    var ne = e.nashornException;
55    if (ne != undefined) {
56        if (ne.fileName !== __FILE__) {
57            fail("incorrect filename in error");
58        }
59        print("thrown @ " + ne.lineNumber);
60    }
61}
62
63// try to print undefined global var..
64try {
65    print(foo);
66} catch (e) {
67    print(e instanceof ReferenceError);
68    print(e.name);
69    print(e.message);
70}
71
72// try to call something that is undefined
73try {
74    Object.foo_method();
75} catch (e) {
76    print(e instanceof TypeError);
77    print(e.name);
78    print(e.message);
79}
80
81// prototypes of Error constructors are not writable
82Error.prototype = {};
83print(Error.prototype.name);
84EvalError.prototype = {};
85print(EvalError.prototype.name);
86RangeError.prototype = {};
87print(RangeError.prototype.name);
88ReferenceError.prototype = {};
89print(ReferenceError.prototype.name);
90SyntaxError.prototype = {};
91print(SyntaxError.prototype.name);
92TypeError.prototype = {};
93print(TypeError.prototype.name);
94URIError.prototype = {};
95print(URIError.prototype.name);
96