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

エラーダイアログを出さない forked from: [FP10.1] Global Error Handler

/**
 * Copyright bkzen ( http://wonderfl.net/user/bkzen )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/ydZ5
 */

// forked from clockmaker's [FP10.1] Global Error Handler
package {
    import com.bit101.components.*;
    import flash.display.*;
    import flash.events.*;
    
    public class Main extends Sprite {
        public function Main() {
            new PushButton(this, 100, 40, "throw new Error", _onBtnClick);
            _radio1 = new RadioButton(this, 100, 90, "UNCAUGHT_ERROR Handler ON", true, _onChange);
            _radio2 = new RadioButton(this, 100, 120, "UNCAUGHT_ERROR Handler OFF", false, _onChange);
            _label = new TextArea(this, 100, 170, "");
            loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, _onUncaughtError);
        }
        private var _label:TextArea;
        private var _radio1:RadioButton;
        private var _radio2:RadioButton;
        
        private function _onChange(e:Event):void {
            if (_radio1.selected)
                loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, _onUncaughtError);
            else
                loaderInfo.uncaughtErrorEvents.removeEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, _onUncaughtError);
            _label.text = "";
        }
        
        private function _onBtnClick(e:MouseEvent):void {
            throw new Error("This is Custom Error No." + (1000 * Math.random() >> 0));
        }
        
        private function _onUncaughtError(event:UncaughtErrorEvent):void {
            
            _label.text = event.error;
            event.preventDefault(); // これでエラーダイアログが出ない
        }
    }
}