Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-09-2007, 02:24 AM   #1
tptompkins
Registered User
 
Join Date: Mar 2007
Location: Minneapolis
Posts: 46
Default Synchronous & Asynchronous Error Handling

I'm starting to put together my error handling framework and have a question around Synchronous & Asynchronous Error Handling.

I've been able to use try{} catch{} blocks for handling synchronous errors and I've also tested asynchronous error handling by adding an event listener to call an error handling method when using the NetConnection class.

What I'm wondering is if it's possible to throw an Error from my asynchronous event error handling method and have a parent method catch it in a try{} catch{} block. I'm assuming it isn't possible since I can't get it to work I can throw the error from the method and have it print in the output window but I can't catch it!

Thanks,

Tommy
tptompkins is offline   Reply With Quote
Old 05-09-2007, 08:53 AM   #2
jsebrech
Joeri Sebrechts
 
Join Date: Apr 2005
Location: Antwerp, Belgium
Posts: 1,465
Default

No, a try / catch block will only catch exceptions that were raised in a deeper level of a thread of execution. When you perform an asynchronous function, this runs as a new thread of execution.

Instead of exceptions you could use event dispatching as a mechanism to inform the necessary classes of the failure.
jsebrech is offline   Reply With Quote
Old 05-09-2007, 02:54 PM   #3
tptompkins
Registered User
 
Join Date: Mar 2007
Location: Minneapolis
Posts: 46
Default

Event dispatching huh? Hmm...I think I'll look into that more. But isn't event dispatching for when you want to dispatch your own "custom" events? Would I still use this if I wanted to handle events such as NetStatusEvent.NET_STATUS?

Tommy
tptompkins is offline   Reply With Quote
Old 05-09-2007, 03:44 PM   #4
Assertnfailure
as[org].addListener(this)
 
Assertnfailure's Avatar
 
Join Date: Dec 2005
Location: LA, California
Posts: 837
Default

You can dispatch custom or built-in events.

You can also do something like:

ActionScript Code:
private function errorHandler(evt:ErrorEvent):void{    this.dispatchEvent(evt); }
Assertnfailure is offline   Reply With Quote
Old 05-09-2007, 04:48 PM   #5
tptompkins
Registered User
 
Join Date: Mar 2007
Location: Minneapolis
Posts: 46
Default

I tried this but it's getting caught in a continuous loop and eventually ends in a stack overflow. Here's my code:

Code:
package 
{    
	import flash.net.NetConnection;    
	import flash.net.ObjectEncoding;   
	import redcart.constants.RCConstants;
	import flash.events.NetStatusEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	public class RCNetConnection extends NetConnection
	{                
		public function RCNetConnection()        
		{        
			try{
				objectEncoding = ObjectEncoding.AMF3; 
				
				var gatewayLocation:String = "http://" + RCConstants.getRCDomain() + "/RedCart/v3/rcFlash/amfphp/gateway.php";
				trace("About to connect to gateway: " + gatewayLocation);
				
				// Connect to gateway
				connect(gatewayLocation);   
				this.addEventListener(NetStatusEvent.NET_STATUS, errorHandler);
			}
			catch (err:Error)
			{
				trace("An error has occurred.");
				trace("ERROR MSG: " + err.message);
			}
		}    
		
		
		private function errorHandler(evt:NetStatusEvent):void
		{
			if(evt.info.level == "error")
			{
				trace("Gateway connection failed: " + evt.info.code);
				dispatchEvent(evt);
			}
			else
			{
				trace("RC Gateway Connection Status: " + event.info.code);
			}
		}
	}
}

I'm guessing that it's getting stuck in a loop because the existing listener keeps seeing the event and calling the errorHandler() method. So what am I doing wrong?

Thanks,

Tommy
tptompkins is offline   Reply With Quote
Old 05-09-2007, 08:59 PM   #6
tptompkins
Registered User
 
Join Date: Mar 2007
Location: Minneapolis
Posts: 46
Default

Nevermind - I got it figured out now. I created a custom event and dispatched that to a method on a custom error manager class.
tptompkins is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Synchronous Socket Connection ced-b ActionScript 3.0 1 04-16-2007 05:28 PM
loadmovie asynchronous problem? davidr222 ActionScript 1.0 (and below) 0 10-10-2005 05:01 PM
Asynchronous Error Handling IceCo ActionScript 2.0 9 05-17-2005 10:05 PM
LoadVars.send() asynchronous? retrotron ActionScript 1.0 (and below) 1 08-19-2004 05:09 PM
asynchronous problem mayam ActionScript 1.0 (and below) 6 12-29-2002 11:14 AM


All times are GMT. The time now is 04:39 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.