Handling Java Exceptions in Flex application

We can invoke Java objects using BlazeDS. For details on how to invoke Java objects from flex visit this URL Invoking Java methods from Flex

It’s a common practice in Java methods to throw Exceptions to indicate that something has failed. These exceptions can be either custom exceptions or inbuilt exceptions. It is also common that we include some message in that exception, which describes the problem caused. Now how do we get that message and display it in Flex applications? What if you want to add some other information to the Exception you are throwing and you want to access that information too?

You can do this :)

BlazeDS will by default serialize any Throwable type object. All you need to do is to access the objects.

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 rootCause object to retrieve the properties of the custom Exception object returned from the Java method.

private function handleException(event:FaultEvent):void{

var errorMessage:ErrorMessage = event.message as ErrorMessage;

Alert.show(errorMessage.rootCause.message);

Alert.show(errorMessage.rootCause.myName);

}

We are adding the above method as fault event handler to the RemoteObject component.

<mx:RemoteObject id=”exceptionObj” destination=”CreatingRpc” result=”handleRPC(event)”

fault=”handleException(event)”/>

Invoking the method in the Java class on button click

<mx:Button label=”Invoke Exception” click=”exceptionObj.throwCheckedException()”/>

You can also use the flex.messaging.MessageException. This class is packaged in the flex-messaging-core.jar. You should throw MessagException instead of MyException or any custom exception created. MessageException provides a property named extendedData, which is a HashMap. You can add any data to this property and access it from the Flex application using ErrorMessage(event.message).extendedData.

That’s it :) let me know if you have problem implementing this :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值