如何在Flex中处理异常java异常..?

时间:2011-05-03 12:27:43

标签: java flex

         I have made the WSDL from java code using the Xfire framework,

这是我的java代码..

public class Test implements TestException {
    public void testException(String check) {
    List<String> list = new ArrayList<String>();
    list.add("ABC");
    list.add("XYZ");
    list.add("PQR");
    list.add("LMNOP");
    list.add("EFGH");
    list.add("Pqrst");

    try 
    {
        if(check(list,check))
            System.out.println(check);
    }
    catch (MyException e) 
    {
        e.printStackTrace();
    }
}

public boolean check(List<String> list,String check) throws MyException {
    if(list.contains(check))
        return true;
    else
        throw new MyException();
}
}

例外类是:

public class MyException extends Exception {
public MyException()
{

}

public String toString()
{
    return "Exception by My Exception.....";
}
}

* 弹性代码:*

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">
<mx:WebService id="testService" wsdl="http://localhost:9090/ExceptionTest/xfire/Test?wsdl" showBusyCursor="true">
    <mx:operation name="testException" result="testExceptionRH(event)" fault="testExceptionFH(event)"/>
</mx:WebService>

<mx:Script>
    <![CDATA[
        import mx.rpc.events.FaultEvent;
        import mx.rpc.events.ResultEvent;

        protected function click_clickHandler(event:MouseEvent):void
        {
            testService.testException(showText.text).send;      
        }

        protected function testExceptionRH(event : ResultEvent) : void
        {
            messageLbl.text = "No Exception.....!!!!";          
        }

        protected function testExceptionFH(event : FaultEvent) : void
        {
            trace(event.fault.faultDetail);
            messageLbl.text = "Exception.....!!!!";
        }

    ]]>
</mx:Script>
<mx:Button x="163" y="70" label="Click" id="click" click="click_clickHandler(event)"/>
<mx:TextInput x="114" y="27" id="showText"/>
<mx:Label id="messageLbl" fontWeight="bold"  x="183" y="99"/> 

现在问题是每当我发送除了列表中的对象之外的对象说“Amit”然后它抛出异常,&amp;所以故障消息必须显示,但我总是得到结果处理程序消息,我已经检查了tomcat的日志,它显示异常(MyException)堆栈跟踪,

我做错了什么.. ???

请提供帮助,在此先感谢

1 个答案:

答案 0 :(得分:1)

您已经在testException方法中捕获了异常,因此它没有传播到您的flex前端。

您应该在catch块中处理异常(例如,进行日志记录),然后再次抛出它。

try {
    // do work...
} catch (Exception e) {
    // handle exception
    e.printStackTrace();

    // include the root cause and propagate the exception to flex
    // using RuntimeException as an example)
    throw new RuntimeException("error msg here", e); 
}