|
1 // Copyright 2009 the Sputnik authors. All rights reserved. |
|
2 // This code is governed by the BSD license found in the LICENSE file. |
|
3 |
|
4 /** |
|
5 * Using "try" with "catch" or "finally" statement in a constructor |
|
6 * |
|
7 * @path ch12/12.14/S12.14_A17.js |
|
8 * @description Creating exceptions within constructor |
|
9 */ |
|
10 |
|
11 var i=1; |
|
12 function Integer( value, exception ) { |
|
13 try{ |
|
14 this.value = checkValue( value ); |
|
15 if(exception) $ERROR('#'+i+'.1: Must be exception'); |
|
16 } |
|
17 catch(e){ |
|
18 this.value = e.toString(); |
|
19 if(!exception) $ERROR('#'+i+'.2: Don`t must be exception'); |
|
20 } |
|
21 i++; |
|
22 } |
|
23 |
|
24 function checkValue(value){ |
|
25 if(Math.floor(value)!=value||isNaN(value)){ |
|
26 throw (INVALID_INTEGER_VALUE +": " + value); |
|
27 } |
|
28 else{ |
|
29 return value; |
|
30 } |
|
31 } |
|
32 |
|
33 // CHECK#1 |
|
34 new Integer(13, false); |
|
35 // CHECK#2 |
|
36 new Integer(NaN, true); |
|
37 // CHECK#3 |
|
38 new Integer(0, false); |
|
39 // CHECK#4 |
|
40 new Integer(Infinity, false); |
|
41 // CHECK#5 |
|
42 new Integer(-1.23, true); |
|
43 // CHECK#6 |
|
44 new Integer(Math.LN2, true); |
|
45 |