View Full Version : I Need to Catch URLVariables.Decode() error
landon6544
05-14-2008, 11:04 PM
I'm loading info from a mySQL database through Flash/AS3 --> PHP then back to Flash. I'm not really certain if we're going to know how many times to try and load from the database. I'm getting an error from URLVariables right now from the constructor (Decode ()) when Php sends me nothing. (I'm out of database info) I would like to error handle this so my program doesn't crash. I looked it up in the documentation and it says that the constructor ( Decode () ) throws "Error" when this particular type of thing occurs. How can I handle this? I'm new to AS3, so I'm not very familiar with error handling.
Thanks,
Landon
TomMalufe
05-15-2008, 02:25 AM
look up try..catch..finally
basicly you write
try{
//the code that might give me an error
} catch (e) {
trace(e); //will trace the error code that you usually get... so maybe you don't want to trace it.
}
The Flash CS3 documentation has this to say:
try..catch..finally statement
Usage try {
// try block
} finally {
// finally block
}
try {
// try block
} catch(error[:ErrorType1]) {
// catch block
} [catch(error[:ErrorTypeN]) {
// catch block
}] [finally {
// finally block
}]
Language Version : ActionScript 3.0
Player Version : Flash Player 9
Encloses a block of code in which an error can occur, and then responds to the error. Exception handling, which is implemented using the try..catch..finally statements, is the primary mechanism for handling runtime error conditions in ActionScript 3.0. When a runtime error occurs, Flash Player throws an exception, which means that Flash Player suspends normal execution and creates a special object of type Error. Flash Player then passes, or throws, the error object to the first available catch block. If no catch blocks are available, the exception is considered to be an uncaught exception. Uncaught exceptions cause the script to terminate.
You can use the throw statement to explicitly throw exceptions in your code. You can throw any value, but the best practice is to throw an object because it provides flexibility and matches the behavior of Flash Player.
To catch an exception, whether it is thrown by Flash Player or by your own code, place the code that may throw the exception in a try block. If any code in the try block throws an exception, control passes to the catch block, if one exists, and then to the finally block, if one exists. The finally block always executes, regardless of whether an exception was thrown. If code within the try block does not throw an exception (that is, if the try block completes normally), the code in the catch block is ignored, but the code in the finally block is still executed. The finally block executes even if the try block exits using a return statement.
A try block must be followed by a catch block, a finally block, or both. A single try block can have multiple catch blocks but only one finally block. You can nest try blocks as many levels deep as desired.
The error parameter specified in a catch handler must be a simple identifier such as e or theException or x. The parameter can also be typed. When used with multiple catch blocks, typed parameters let you catch multiple types of error objects thrown from a single try block.
If the exception thrown is an object, the type will match if the thrown object is a subclass of the specified type. If an error of a specific type is thrown, the catch block that handles the corresponding error is executed. If an exception that is not of the specified type is thrown, the catch block does not execute and the exception is automatically thrown out of the try block to a catch handler that matches it.
If an error is thrown within a function, and the function does not include a catch handler, Flash Player exits that function, as well as any caller functions, until a catch block is found. During this process, finally handlers are called at all levels.
landon6544
05-15-2008, 03:23 PM
Thank you so much for taking the time to write that out for me. I had already looked up try...catch and had tried to implement it, but it wasn't working for me. After reading your post, I think I understood it quite better. I tried a few more things, and got it to work properly.
I needed to put the try..catch statement within my (Event.Complete) handler for my URLLoader, instead of around my loading statements. I should have read the error box more carefully. I'm still just getting where I can read them with any measure of success.
Thanks Again!
Landon
Ciubhran
06-18-2009, 01:41 PM
I got this exact problem. Tried everything, but nothing works. I want the program to NOT crash when the webserver is offline.
Here is the code:
public function UpdateWorldPosition() {
var url_send = "http://randomurl.com/random_page.php.php?ac=" + Math.floor(Math.random() * 1000000) ;
trace(url_send);
var variables = new URLVariables();
var req_send = new URLRequest(url_send);
var loader_send = new URLLoader();
var date = new Date();
variables.id = this.ID;
variables.x_pos = 10 + Math.floor((this.RealX - 10) / 20) * 20;
variables.y_pos = 10 + Math.floor((this.RealY - 10) / 20) * 20;
variables.rid = this.RequestID;
variables.ts = Math.floor( date.getTime() / 1000 );
req_send.data = variables;
req_send.method = URLRequestMethod.POST;
loader_send.dataFormat = URLLoaderDataFormat.VARIABLES;
loader_send.addEventListener(Event.COMPLETE, EventCompleteWorldPosition);
loader_send.load(req_send);
}
private function EventCompleteWorldPosition(e:Event) {
try {
this.RequestID = e.target.data.request_id;
trace("world pos: success");
} catch(err:Error) {
trace("world pos: failure");
trace("error");
this.ErrorMsg("Your connection to the server has been lost");
this.TurnOffListeners = true;
}
}
I can't get an error to be thrown. The event complete handler succeeds, but it halts for about 10 or 15 seconds, then crashes and it hasn't even catched anything.
Ciubhran
06-18-2009, 03:02 PM
Solved the problem today.
If you use the dataFormat "VARIABLES", which is the most comfortable way to handle data that is returned by your PHP page, then you can't catch (for some reason) any IOErrorEvent.
So what I did was, I changed to dataFormat "TEXT", then added an event listener to handle IOErrorEvents to my loader.
If the page is not found (webserver is offline) then an IOErrorEvent will spawn correctly (if dataFormat = TEXT). But like I said, this doesn't work with dataFormat VARIABLES, and it is a known bug in Flash Player 10.
I simply just added a string parser for all my returned variables in the Event.Complete event handler to handle the string (evt.target.data) that the PHP page now instead returns.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.