Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.

Hi,

today I’ve come across the “Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.” error, but becuase (as always) the Flash Player errors are really stripped from any information that might help to pint point the problem I couldn’t find it. After doing some searching I’ve found that the img tag on the html enabled textfield will cause this error when image will not load properly.

Sample with problem:

package 
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.text.TextField;
	
	/**
	 * <p>Sample to show Unhandled IOErrorEvent</p>
	 * @author Lukasz 'Severiaan' Grela
	 */
	public class Main extends Sprite 
	{
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			//
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			//
			var htmlTf:TextField = new TextField();
				htmlTf.htmlText = "<p>Text and image: <img src='not_found.jpg' /></p>";
			//
		}
	}		
}

I wasn’t able to find the solution to pre FP10.1 version as any workarounds just failed e.g. I was able to listen for Event.ADDED and Event.REMOVED on textfield to check when something is added and removed, and by checking if it is Loader I was able to add requested IOErrorEvent handler (and remove it), however the error was still thrown! See modified sample.

package 
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.text.TextField;
	
	/**
	 * <p>In this sample you can see that we have attached the IOErrorEvent handler to the image loader but still the Unhandled error is thrown</p>
	 * @author Lukasz 'Severiaan' Grela
	 */
	public class Main extends Sprite 
	{
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			//
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			//
			var htmlTf:TextField = new TextField();
			
				htmlTf.addEventListener(Event.ADDED, objectAdded, false, 0, true);
				htmlTf.addEventListener(Event.REMOVED, objectRemoved, false, 0, true);
			
			
				htmlTf.htmlText = "<p>Text and image: <img src='not_found.jpg' /></p>";
			//
		}
		
		protected function objectRemoved(e:Event):void 
		{
			if (e.target is Loader)
			{
				var li:LoaderInfo = (e.target as Loader).contentLoaderInfo;
				li.addEventListener(IOErrorEvent.IO_ERROR, handleLoadEvents, false, 0, true);
				li.addEventListener(Event.COMPLETE, handleLoadEvents, false, 0, true);
			}
		}
		
		
		protected function objectAdded(e:Event):void 
		{
			if (e.target is Loader)
			{
				var li:LoaderInfo = (e.target as Loader).contentLoaderInfo;
				li.removeEventListener(IOErrorEvent.IO_ERROR, handleLoadEvents, false);
				li.removeEventListener(Event.COMPLETE, handleLoadEvents, false);
			}
		}
		
		protected function handleLoadEvents(e:Event):void 
		{
			var li:LoaderInfo = (e.target as LoaderInfo);
				li.removeEventListener(IOErrorEvent.IO_ERROR, handleLoadEvents, false);
				li.removeEventListener(Event.COMPLETE, handleLoadEvents, false);
			switch (true) 
			{
				case e is Event && e.type == Event.COMPLETE:
					
					break;
				case e is IOErrorEvent && e.type == IOErrorEvent.IO_ERROR:
					trace(e);//at least you will see the detailed error
					break;
			}
		}
	}		
}

If you are targetting the FP 10.1 and above you can use the loaderInfo’s new property uncaughtErrorEvents and register handler to listen for UncaughtErrorEvent.UNCAUGHT_ERROR event.

Let’s add following to the previous sample

// in the constructor
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, handleUncaughtErrors, false, 0, true);
//
//
protected function handleUncaughtErrors(e:UncaughtErrorEvent):void 
{
	trace(e);
}

If you will run the sample you will notice that the Unhandled IOErrorEvent is still dispatched, why? It turns out that you have to prevent default action to stop uncaught error dialog from appearing in debugger runtime versions. So modify the handler as follows:

//
protected function handleUncaughtErrors(e:UncaughtErrorEvent):void 
{
	e.preventDefault();
	trace(e);
}

What about the FP10.0 and below, nothing, if you know how to solve it than please let me know, But at least you will know where to look for the issue if this problem will happen to you:)

Best regards

This entry was posted in actionscript, flash and tagged , , , , . Bookmark the permalink.