Home | History | Annotate | Download | only in Statements
      1 /**
      2  *  File Name:          try-006.js
      3  *  ECMA Section:
      4  *  Description:        The try statement
      5  *
      6  *  Throw an exception from within a With block in a try block.  Verify
      7  *  that any expected exceptions are caught.
      8  *
      9  *  Author:             christine (at) netscape.com
     10  *  Date:               11 August 1998
     11  */
     12     var SECTION = "try-006";
     13     var VERSION = "ECMA_2";
     14     var TITLE   = "The try statement";
     15 
     16     startTest();
     17     writeHeaderToLog( SECTION + " "+ TITLE);
     18 
     19     var tc = 0;
     20     var testcases = new Array();
     21 
     22     /**
     23      *  This is the "check" function for test objects that will
     24      *  throw an exception.
     25      */
     26     function throwException() {
     27         throw EXCEPTION_STRING +": " + this.valueOf();
     28     }
     29     var EXCEPTION_STRING = "Exception thrown:";
     30 
     31     /**
     32      *  This is the "check" function for test objects that do not
     33      *  throw an exception
     34      */
     35     function noException() {
     36         return this.valueOf();
     37     }
     38 
     39     /**
     40      *  Add test cases here
     41      */
     42     TryWith( new TryObject( "hello", throwException, true ));
     43     TryWith( new TryObject( "hola",  noException, false ));
     44 
     45     /**
     46      *  Run the test.
     47      */
     48 
     49     test();
     50 
     51     /**
     52      *  This is the object that will be the "this" in a with block.
     53      */
     54     function TryObject( value, fun, exception ) {
     55         this.value = value;
     56         this.exception = exception;
     57 
     58         this.valueOf = new Function ( "return this.value" );
     59         this.check = fun;
     60     }
     61 
     62     /**
     63      *  This function has the try block that has a with block within it.
     64      *  Test cases are added in this function.  Within the with block, the
     65      *  object's "check" function is called.  If the test object's exception
     66      *  property is true, we expect the result to be the exception value.
     67      *  If exception is false, then we expect the result to be the value of
     68      *  the object.
     69      */
     70     function TryWith( object ) {
     71         try {
     72             with ( object ) {
     73                 result = check();
     74             }
     75         } catch ( e ) {
     76             result = e;
     77         }
     78 
     79         testcases[tc++] = new TestCase(
     80             SECTION,
     81             "TryWith( " + object.value +" )",
     82             (object.exception ? EXCEPTION_STRING +": " + object.valueOf() : object.valueOf()),
     83             result );
     84     }
     85