Archive for the 'Flex Error' Category

22
Apr

Flex Error Handling Example

 

This example is associated with the blog “Dustin’s Software
Cogitations and Speculations.”

The Flash Players, regular and debugger, can be downloaded
at http://www.adobe.com/support/flashplayer/downloads.html. –>

<mx:Script>
import mx.utils.ObjectUtil;

/**
* Test Flex exception handling.
*/
public function testException():void
{
try
{
intentionallyThrowException();
}
catch (error:Error)
{
stackTraceText.text = ObjectUtil.toString(error.getStackTrace());
messageText.text = error.message;
toStringText.text = error.toString();
nameText.text = error.name;
errorIdText.text = String(error.errorID);
}
finally
{
// I get called whether an exception is caught or not.
}
}

/**
* Intentionally throw an exception for use in Flex exception
* testing.
*/
public function intentionallyThrowException():void
{
throw new SyntaxError(“That was some bad syntax!”);
}
</mx:Script>

<mx:VBox id=”mainDisplay”>
<mx:Form>
<mx:FormItem id=”stackTraceItem” label=”Exception Stack Trace”>
<mx:Text id=”stackTraceText” />
</mx:FormItem>
<mx:FormItem id=”messageItem” label=”Message”>
<mx:Text id=”messageText” />
</mx:FormItem>
<mx:FormItem id=”toStringItem” label=”toString”>
<mx:Text id=”toStringText” />
</mx:FormItem>
<mx:FormItem id=”nameItem” label=”Name”>
<mx:Text id=”nameText” />
</mx:FormItem>
<mx:FormItem id=”errorIdItem” label=”Error ID”>
<mx:Text id=”errorIdText” />
</mx:FormItem>
</mx:Form>
</mx:VBox>

</mx:Application>

A significant portion of the above code is actually comments. If you remove the explanatory comments, the code is pretty small. Either way, the code is straightforward, especially if you have written Java exception handling code before. Continue reading ‘Flex Error Handling Example’

22
Apr

Exception Handling with BlazeDS and Flex

 

Create a Java Class that extends RuntimeException.

package com.flex3.exception;
public class FlexException extends RuntimeException
{
public FlexException(String message)
{
super(message);
}
}

2. Create a matching flex class called FlexException, and put the usual RemoteObject tag in their so BlazeDS will know what to look for. Also remember in actionscript, each object must be instantiated at least once, so be sure to do that somewhere with FlexException.

3. In Java, whenever there is a known exception, or some error where the end user needs to be shown a message, throw a FlexException and pass in the message the user should see in the constructor. Let’s say the user just entered an invalid password. throw new FlexException(”Wrong Password Entered, Please Try Again.”); Continue reading ‘Exception Handling with BlazeDS and Flex’

22
Apr

Exception Handling

 

Exceptions

Table of Contents

10.1. The Exception-Handling Cycle
10.2. Handling Multiple Types of Exceptions
10.2.1. Determining Exception Type Granularity
10.3. Exception Bubbling
10.3.1. Uncaught Exceptions
10.4. The finally Block
10.5. Nested Exceptions
10.5.1. A Nested Exception Bug
10.6. Control Flow Changes in try/catch/finally
10.7. Limitations of Exception Handling in ActionScript 2.0
10.7.1. No Checked Exceptions
10.7.2. No Built-in Exceptions
10.7.3. Exception Performance Issues
10.8. From Concepts to Code

Throughout this book, we’ve encountered plenty of compile-time errors—errors reported in the Output panel when a movie is exported. If a compile-time error occurs in an ActionScript 2.0 program, compilation fails and Flash won’t generate a .swf file to execute. But not all ActionScript errors occur at compile time. Some errors don’t occur until runtime, and they may not cause the program to fail completely. For example, suppose we attempt to load an XML file from disk, but the file is not found. If the movie is running in Test Movie mode, the failed load causes the Output panel to display an error message—but the movie continues to run. The following code demonstrates:

 

// If the specified file doesn't exist... var xmlDoc:XML = new XML( ); xmlDoc.load("http://www.somenonexistentsite.com/someNonExistentFile.xml");

// ...the Output panel displays: Error opening URL "http://www.somenonexistentsite.com/someNonExistentFile.xml"

 

In an ideal world, we’d like to be able to recover from nonfatal error conditions such as a file-not-found. We’d like to tell the user there was a problem loading the file and perhaps display the problematic filename.

Unfortunately, in ActionScript there are precious few built-in runtime errors and, what’s worse, there’s no standard error-handling mechanism for dealing with the errors that do occur at runtime—at least, not for the errors that are generated by ActionScript itself. Most errors in ActionScript occur in the form of custom error codes and return values. For example, a method might return the value -1, false, or nullto indicate that some operation failed. This requires us to write different, individualized code for each kind of error generated by ActionScript. Continue reading ‘Exception Handling’

22
Apr

Handling Java Exceptions in Flex application

 

Accessing the Throwable object in Flex

RemoteObject component invokes the fault event when an error occurs while remote method invocation. The fault event handler is provided with the FaultEvent object. This FaultEvent object has property named message of type mx.messaging.messages.ErrorMessage. The message property holds the Throwable object from the Java method in the rootCause property. We need to use this rootCause property to retrieve the properties which are set to the Throwable object in Java. All the public properties from the Throwable object are available.

We will see a sample application. In this application I am creating a custom Exception and adding a getter method to that, which will return my custom data. From the Flex application I will access both the error message and the custom data.

MyException.java

public class MyException extends Exception {

public MyException(String message) {

super(message);

}

public String getMyName(){

return “Sujit Reddy G”;

}

}

 

Method throwing exception

This method will throw the custom exception created above, add this method to a Java class. Invoke the below method using RemoteObject component in Flex.

 

public void throwCheckedException() throws Exception{

throw new MyException(”This is a checked exception”);

}

Reading values in Flex application

We add the method below as the fault event handler to the RemoteObject component in the Flex application. You can see that we accessed the rootCauseobject to retrieve the properties of the custom Exception object returned from the Java method. Continue reading ‘Handling Java Exceptions in Flex application’

05
Jul

Data Binding, and Regular Expressions

Parsing XML has never been fun. Java programmers use way too many different parsers and APIs.
Java 6 includes Java Architecture for XML Binding (JAXB 2.0) and the implementation of the XML
Data Binding Specification (JSR 31) that maps JavaBeans and XML data. To be more accurate, JAXB
2.0 offers two-directional mapping between JavaBeans and XML Schema (see the javax.xml.bind
package). Java 6 comes with a new tool called xjc that takes an XML Schema as an input and generates
the required JavaBeans as an output.
ActionScript 3.0 supports E4X, which is an ECMA standard for working with XML (see http://www.
ecma-international.org/publications/files/ECMA-ST/ECMA-357.pdf ). It’s very powerful and yet
simple to use. You can forget about these SAX and DOM parsers. E4X is a step towards making XML
a programming language.
For example, an MXML program can read the XML file people.xml (or any other XML source) shown
in Listing 4.23 into a variable with only one line (without worrying about error processing):
<mx:XML id=”myXmlFile” source=”people.xml”/>
You’ll need another line of code to populate the data grid using Flex data binding (remember, tying
the data from a source to a destination). In our case myXmlFile is the source that populates the data
grid aka destination:
<mx:DataGrid dataProvider=”{myXmlFile.person}”>
This line means that each element <person> will populate one row in the data grid. Let’s make the
XML from Listing 4.23 a bit more complex: we’ll introduce nesting in the name element. Now it
consists of separate <first> and <last> elements.
<?xml version=”1.0” encoding=”UTF-8”?>
<people>
<person>
<name>
<first>Yakov</first>
<last>Fain</last>
</name>
<age>22</age>
<skills>java, HTML, SQL</skills>
CHAPTER 4
RIA WITH ADOBE FLEX AND JAVA 123
</person>
<person>
<name>
<first>Victor</first>
<last>Rasputnis</last>
</name>
<age>21</age>
<skills>PowerScript, JavaScript, ActionScript</skills>
</person>
<person>
<name>
<first>Anatole</first>
<last>Tartakovsky</last>
</name>
<age>20</age>
<skills>SQL, C++, Java</skills>
</person>
</people>
Listing 4.32 The XML file FarataSystems_skills.xml
Our goal is to produce a window that looks like this:
Figure 4.9 A data grid populated from people.xml
Please note that we also want to concatenate the values from the <first> and <last> XML elements
for the data grid’s Name column. A small program in Listing 4.30 does exactly this. The fullName
method concatenates the first and last names, and since we specified the labelFunction property
in the name column, the data rendering will be controlled by the fullName() function. We’ll return
to labelFunction in Chapter 11.
<?xml version=”1.0” encoding=”utf-8”?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”>
Learning Flex Through Applications
124 RIA WITH ADOBE FLEX AND JAVA
<mx:XML id=”myXmlFile” source=”FarataSystems_Skills.xml”/>
<mx:Label text=”Java Developers” fontSize=”18”/>
<mx:DataGrid dataProvider=”{myXmlFile.person}” width=”500”>
<mx:columns>
<mx:DataGridColumn dataField=”name” headerText=”Name” labelFunction=”fullName”/>
<mx:DataGridColumn dataField=”age” headerText=”Age”/>
<mx:DataGridColumn dataField=”skills” headerText=”Skills”/>
</mx:columns>
</mx:DataGrid>
<mx:Script>
<![CDATA[
private function fullName(item:Object, column:DataGridColumn):String {
return item.name.first + “ “ + item.name.last;
}
]]>
</mx:Script>
</mx:Application>
Listing 4.33 Populating a data grid from an XML file
The next step is to add regular expressions to filter the data while populating the data grid. There’s
a nice example of predicate filtering with E4X and RegExp in Darron Schall’s blog at http://www.
darronschall.com/weblog/archives/000214.cfm.
Let’s imagine that a recruiter wants to do a quick search in our XML file to identify people with Java
skills. A small one-line change will do this trick, or at least will be a step in the right direction.
The RegExp class lets you create an instance of the object per the specified pattern, and then find
the substring(s) with this pattern and perform the manipulations with the found substrings, if
any.
In AS3, you can create an instance of the RegExp by using a familiar syntax with a constructor. For
example:
var javaPattern:RegExp = new RegExp(“Java”, “i”);
This is a pattern for finding occurrences of “Java,” ignoring the letter case.
Here’s another way of creating this instance:
var javaPattern:RegExp = /Java/i;
We’ll use the latter syntax by feeding the E4X output to this RegExp instance and the result will be
used as a data provider for the data grid. Let’s modify the MXML code for the <mx:DataGrid> tag to
CHAPTER 4
RIA WITH ADOBE FLEX AND JAVA 125
apply this regular expression to the XML element called skills:
<mx:DataGrid dataProvider=”{myXmlFile.person.(/Java/.test( skills ))}” >
In the line above, /Java/ creates an instance of the RegEx object and the test(skills) method will
ensure that only those XML elements that contain Java are included in the myXmlFile. Now the
resulting window will look as follows:
Figure 4.10 Displaying FarataSystems_skills.xml with Java skills
We still don’t like a couple of things here. First, this output didn’t include Yakov Fain because the
word Java was written in lower case in his skills element. Adding the ignore case option “i” to our
RegExp instance will help:
<mx:DataGrid dataProvider=”{myXmlFile.person.(/Java/i.test(skills))}” >
The output will again look like Figure 4.9.
The next step is to filter out people who were included in this list just because of JavaScript, which
has very little to do with Java. One of the ways to do this is by requesting that there should be a
space or a comma in the regular expression after the word Java:
<mx:DataGrid
dataProvider=”{myXmlFile.person.(/Java? ?,/i.test(skills))}” >
Now we’ve lost both Victor and Anatole. Even though Anatole knows Java, there’s no space or comma
after the word Java in his list of skills. Adding an OR (|) condition that means we’re also interested
in people with the word Java as the last word in the string will help.
<mx:DataGrid
dataProvider=”{myXmlFile.person.(/Java? ?, | Java$/i.test(skills))}”
Learning Flex Through Applications
126 RIA WITH ADOBE FLEX AND JAVA
Figure 4.11 Finding Java programmers
Today, E4X doesn’t support XML Schema, and all the XML elements are returned as text, but it’ll
change in future versions of the ECMAScript for XML. Meanwhile, the implementation of E4X by
any programming language makes it more appealing to developers who have to deal with XML.



Get Adobe Flash playerPlugin by wpburn.com wordpress themes