In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

throw で条件分岐

おもしろい。
Get Adobe Flash player
by bkzen 06 Apr 2012
/**
 * Copyright bkzen ( http://wonderfl.net/user/bkzen )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/gNSC
 */

package  
{
    import flash.events.Event;
    import flash.events.MouseEvent;
    import com.actionscriptbible.Example;
    
    /**
     * throw 式条件分岐
     * @author jc at bkzen
     */
    public class Test90 extends Example 
    {
        private var cnt: uint;
        
        public function Test90() 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e: Event = null): void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            //
            trace("click");
            stage.addEventListener(MouseEvent.CLICK, onClick);
        }
        
        private function onClick(e:MouseEvent):void 
        {
            try { foo(); }
            catch (str: String) { trace("String ", str); }
            catch (i: int) { trace("int", i); }
            catch (err: Error) { trace("Error", err); }
            catch (c: Class) { trace("Class", c); }
            catch (o: Object) { trace("Object", o); }
        }
        
        private function foo():void 
        {
            switch (cnt++%5)
            {
                case 0:
                    throw { "toString": function(): String { return "[MyObject]" }};
                    break;
                case 1:
                    throw "'Error'";
                    break;
                case 2:
                    throw 1234;
                    break;
                case 3:
                    throw new Error("new Error");
                    break;
                case 4:
                    throw Class;
                    break;
            }
        }
        
    }

}