Try...catch for getQualifiedClassName

How do I check if a class exists? and handle the error if it doesn’t?

if the class exists the code below traces
// no Error
// Finally!

However if it doesnt exist i get a compiler error
// 1120: Access of undefined property cm_button_bg_g

I was leaad to believe the try catch statement should handle the compiler error… Am I going about this the wrong way?


try {
   getQualifiedClassName(cm_button_bg_g) == "cm_button_bg_g";
   trace("no Error ");
} catch(error:Error) {
   trace("Error catch: " + error);
} finally {
   trace("Finally!");
}

Any help would be greatly appreciated :slight_smile:

getQualifiedClassName() doesn’t throw any errors. The error you’re getting isn’t a run-time error, it’s compile-time… Sure, if there’s no cm_button_bg_g variable it won’t compile…
Did you mean getDefinitionByName()? it may throw ReferenceError.

getQualifiedClassName will return the classname of the object you pass to it. In this case you want to check if a class is actually existing right?

Try this on for size;


var classExists:Boolean = true;
var classNameToTest:String = "flash.display.Sprite";

try 
{
   var k = getDefinitionByName(classNameToTest);
} 
catch(e:Error) 
{
   trace("Statement threw an error: " + e);
   classExists = false;
} 

classExists ? trace("Your class exists") : trace("Your class doesn't exist");

Oh man, words can not describe how helpful those posts are! works a treat now.

I also know to look for the throws heading in help when using try catch now.

thanks again :slight_smile:

You’re welcome :smiley: