Get $500+ of the best After Effects files, video templates and music for only $20!
Quick-Tip: Throwing Errors the Clean Way

Quick-Tip: Throwing Errors the Clean Way

This entry is part 3 of 10 in the How to Fix Bugs in Flash Session
« PreviousNext »

There comes a time while developing, especially when developing for other developers, that you think “I should really anticipate this error”. This Quick Tip will show you how to do it — the clean way.


Step 1: Throwing

Often, you’ll try to anticipate a bug but end up just tracing something out. Whilst debugging you would find it really useful to throw an exception to make your code stop from executing further, or give some more information about why the error is happening. To do that you need to throw an error to the user, meaning Flash’s debug player will be able to catch it, as will the console when exporting directly from Flash Professional.

Let’s say I’m building a User class; it’s a static class with parameters like name and gender.

Now let’s say I want to catch if the user inputs something other than M or F in his/her gender:

package {

	public class User1 {
		private static var _name:String;
		private static var _gender : String;

		//SETTERS
		public static function set name(name:String):void{
			_name = name;
		}
		public static function set gender(gender:String):void{
			gender = gender.toUpperCase();
			if(gender != "F" && gender !="M"){
				throw new Error("Gender must be F or M");
			}else{
				_gender = gender;
			}
		}
		public static function get name():String{
			return _name;
		}
		public static function get gender():String{
			return _gender;
		}
	}
}

By submitting something like User1.gender = "multi", I would been thrown an error in my debugger specifying that my gender is invalid.


Step 2: Try and Catch the Error

Grabbing this concept and developing it further, I can also build custom error classes.

Let’s say I have a UConnection class that handles User Connection activity like login. This login function sends information to a php method that returns a user id (in this case I’ll call it uid) and you need to anticipate the uid being invalid (meaning the login could not be performed).

What you would need to create first is a custom error class, a UIDError class.

package{
	public class UIDError extends Error{
		public function UIDError():void{
			super("User ID returned invalid");
		}
	}
}

Secondly a class that anticipates and handles the exception:

package{

	public class User{
			private static var _uid:int;

		//SETTER
		public static function set uid(uid:int):void{
			if(uid == 0){
				throw new UIDError();
			}else{
				_uid = uid;
			}
		}

		//GETTER
		public static function get uid():int{
			return _uid;
		}
	}
}

Finally the implementation:

//bear in mind this is pseudo code,
//it will not work as I don't have a login.php
//and I'm not sending any variable through post,
//for a working version refer to the samples provided

public function login(un:String;pw:String):void{
	urlloader:URLLoader = new URLLoader();
	urlloader.addEventListener(Event.COMPLETE,onLogin)
	urlloader.load(new URLRequest("login.php"));
}

private function onLogin(e:Event){
	try{
		User.uid = e.target.data
	}catch(e:UIDError){
		trace("login invalid");
	}
}

The code inside the catch block runs if it notices any errors being thrown inside the try block. In this case, if a UIDError is thrown when attempting to set User.uid, then "login invalid" will be traced. If any other type of error gets thrown, then Flash Player will react in the way it normally does.


Conclusion

So there you have it, a clean, simple way of error handling your code, for you and your coworkers’ debugging pleasure.

I hope you liked this quick tip, thanks for reading!

Tags: Tips
Add Comment

Discussion 2 Comments

  1. André says:

    It not seems to be much clearer, only removed the string from the Error but now we have a new class just to trhow the custom error, I think better the old way, anyway, thanks!

    • Guyllaume says:

      In his exemple, I’ll admit it might seem useless, but in larger more complex programs, having a specific class throwing the errors can and will make it easier to handle/understand.

      With a specific Error class, you can throw a different error type, allowing the user to better understand the problem, or control how his code reacts to errors.

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.