现在的位置: 首页 > 综合 > 正文

flex_(4)CustomErrors自定义错误类

2013年10月10日 ⁄ 综合 ⁄ 共 5096字 ⁄ 字号 评论关闭

效果:

=>CustomErrorsApp.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
  xmlns:s="library://ns.adobe.com/flex/spark" 
  xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
  pageTitle="TheStudioOfCenyebao" creationComplete="creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import com.cen.programmingas3.errors.FatalError;
import com.cen.programmingas3.errors.Validator;
import com.cen.programmingas3.errors.WarningError;

import mx.events.FlexEvent;

[Bindable]
public var employeeXML:XML;// 员工XML对象;

/**
* 初始函数
*/
protected function creationCompleteHandler(event:FlexEvent):void
{
/*初始对象*/
employeeXML = 
<employee id="12345">
            <firstName>John</firstName>
            <lastName>Doe</lastName>
            <costCenter>12345</costCenter>
            <costCenter>67890</costCenter>
            </employee>;

statusText.text = "请点击验证按钮来验证员工信息!";

// 隐藏按钮
hideButtons();
}

/**
* 隐藏按钮
*/
private function hideButtons():void
{
yesButton.visible = false;
noButton.visible = false;
}

/**
* 显示按钮
*/
private function showButtons():void
{
yesButton.visible = true;
noButton.visible = true;
}

/**
* 验证数据
*/
private function validateData():void
{
try
{
/*临时xml对象*/
var tempXml:XML = XML(xmlText.text);

/*验证*/
Validator.validateEmployeeXML(tempXml);

/*如果上边验证没抛出异常,将运行下面赋值语句*/
statusText.text = "员工信息验证通过!";

catch(error:FatalError) 
{
showFatalError(error);
}
catch(error:WarningError)
{
showWarningError(error);
}
catch(error:Error)
{
showGenericError(error);
}
}

/**
* 显示致命错误信息
*/
private function showFatalError(error:FatalError):void
{
/*错误信息*/
var message:String = error.message + "\n\n";
var title:String = error.getTitle();

/*显示*/
statusText.text = message + "  " + title + "\n\n应用程序已经停止!";

this.xmlText.enabled = false;
this.validateBtn.enabled = false;

hideButtons();
}

/**
* 显示警告错误信息
*/
private function showWarningError(error:WarningError):void
{
var message:String = error.message + "\n\n" + "要退出应用程序吗?";
var title:String = error.getTitle();

/*显示按钮*/
showButtons();

// 显示错误信息
statusText.text = message + "  " + title;
}

/**
* 显示普通错误信息
*/
private function showGenericError(error:Error):void
{
statusText.text = error.message + "\n\n遇到未知错误:" + error.name;
}

/**
* 按钮选择
*/
private function closeHandler(event:MouseEvent):void
{
switch(event.target)
{
case yesButton:
{
showFatalError(new FatalError(9999));
break;
}
case noButton:
{
statusText.text = "";
hideButtons();
break;
}
}
}

]]>
</fx:Script>

<s:HGroup width="100%" horizontalAlign="center" verticalAlign="middle" horizontalCenter="0" verticalCenter="0">
<s:VGroup height="100%" horizontalAlign="right">
<s:TextArea id="xmlText" text="{employeeXML}" width="250" height="200" fontSize="12"/>
<s:Button id="validateBtn" width="100" label="验证" click="{validateData()}"/>
</s:VGroup>

<s:VGroup height="100%" horizontalAlign="right">
<s:TextArea id="statusText" width="250" height="200" fontSize="12"/>
<s:HGroup>
<s:Button id="yesButton" width="65" label="是" click="{closeHandler(event)}"/>
<s:Button id="noButton" width="65" label="否" click="{closeHandler(event)}"/>
</s:HGroup>
</s:VGroup>
</s:HGroup>
</s:Application>

=>ApplicationError.as

package com.cen.programmingas3.errors
{

/**
* ApplicationError自定义错误基类
* @author cen
*/
public class ApplicationError extends Error
{
/**
* 属性*/
/*静态属性_错误程度(警告)*/
internal static const WARNING:int = 0;
/*静态属性_错误程度(致命)*/
internal static const FATAL:int = 1;
/*错误ID*/
public var id:int;
/*错误严重程度*/
public var severity:int;
/*错误基类内置错误对象*/
private static var messages:XML;


/**
* constructor
*/
public function ApplicationError()
{
messages = 
<errors>
<error code="9000">
<![CDATA[员工必须得隶属成本中心!]]>
</error>

<error code="9001">
<![CDATA[员工只能隶属一个成本中心!]]>
</error>

<error code="9002">
<![CDATA[每位员工只能拥有唯一一个社会安全号码!]]>
</error>

<error code="9999">
<![CDATA[应用程序已经被迫中止!]]>
</error>
</errors>
}

/**
* 通过错误ID获取错误信息
* @param id
*/
public function getMessageText(id:int):String {
var message:XMLList = messages.error.(@code == id);
return message[0].text();
}

/**
* 返回错误标题信息(错误ID)
* @return 
*/
public function getTitle():String
{
return "Error #" + id;
}

/**
* 覆盖toString方法_自定义字符串输出
* @return 
*/
public function toString():String
{
return "[APPLICATION ERROR #" + id + "] " + message
}
}
}

=>WarningError.as

package com.cen.programmingas3.errors
{
/**
* WarningError警告错误子类
* @author cen
*/
public class WarningError extends ApplicationError
{

/**
* constructor
*/
public function WarningError(errorID:int)
{
/*根据父类内置错误ID来指定子类错误ID*/
id = errorID;

/*严重程度*/
severity = ApplicationError.WARNING;

message =  super.getMessageText(errorID);
}

public override function getTitle():String
{
return "Error #" + id + " -- Warning;";
}


public override function toString():String
{
return "[WARNING ERROR #" + id + "] " + message;
}
}
}

=>FatalError.as

package com.cen.programmingas3.errors
{
/**
* FatalError致命错误子类
* @author cen
*/
public class FatalError extends ApplicationError
{

/**
* constructor
*/
public function FatalError(errorID:int)
{
/*指定错误ID*/
id = errorID;

/*错误严重程度*/
severity = ApplicationError.FATAL;

/*通过ID获取错误信息*/
message = getMessageText(errorID);
}

public override function getTitle():String
{
return "Error #" + id + " -- FATAL";
}

public override function toString():String
{
return "[FATAL ERROR #" + id + "] " + message;
}
}
}

=>Validator.as

package com.cen.programmingas3.errors
{
/**
* Validator验证类
* @author cen
*/
public class Validator
{
/**
* validateEmployeeXML方法
* @param employee
*/
public static function validateEmployeeXML(employee:XML):void
{
if (employee.costCenter.length() < 1) {
throw new FatalError(9000);
}

if(employee.costCenter.length() > 1) {
throw new WarningError(9001);
}

if(employee.ssn.length() != 1) {
throw new FatalError(9002);
}
}
}
}

抱歉!评论已关闭.