8.6 Error Handling in Flash Remoting with .NET

One of the true marks of a good programmer is how she handles errors in the code. Programmers coming from a Classic ASP background likely have very brief experience with good error handling. Flash taps directly into .NET's robust error-handling mechanisms. This section discusses how to trap errors and expose them to your Flash application. We also discuss different error-handling techniques and how to use them with Flash. For related information, see Section 6.5.

8.6.1 Catch Me If You Can

The .NET languages support the try/catch construct for error handling. When an error occurs, code execution stops and the .NET Framework raises an exception. The following C# snippet includes code that may raise an exception, depending on the value of divisor:

int divideIt (int numerator, int divisor) {
  return = numerator / divisor;
}

This code will raise a DivideByZeroException if divisor is 0, because dividing by zero is a mathematical impossibility. Though you can avoid the exception by checking the divisor for a zero value before performing the division, there are situations where anticipating an exception is difficult if not impossible. For example, if you write code that connects to a remote SQL Server and the remote server reboots while your code is running, you're going to find yourself with a big fat exception.

Luckily, handling exceptions is easy. You can handle the DivideByZeroException by adding a try/catch block, as follows. If the exception is raised (which occurs when divisor is 0), the function simply returns 0:

int divideIt (int numerator, int divisor) {
  try {
    return = numerator / divisor;
  } catch (Exception ex) {
    return = 0;
  }
}

Any code the invokes the preceding function will never see an exception, because the function's error handling is self-contained. However, there are times when server-side code should transfer the responsibility for exception handling to the client side. As discussed in Section 4.6 in Chapter 4, you have several choices about how to handle errors, including throwing a custom exception.

Simply throw the exception when you detect the error condition of interest:

int divideIt (int numerator, int divisor) {
  if (divisor == 0) {
    throw new Exception("Divisor cannot be zero");
  } else {
    return numerator / divisor;
  }
}

Therefore, when the divisor parameter is 0, the divideIt( ) function throws an exception with a custom message detailing the problem. Utility functions are prime candidates for this type of error handling. It allows developers using that code to see any problems early in the development phase, and the remaining code in the utility function can assume that it won't have to deal with any invalid values.

8.6.2 Exceptional Flash

What does all this mean to Flash applications? Flash Remoting for .NET allows you to call a remote method, and if an unhandled exception is encountered, you can react accordingly.

In the earlier ActionScript examples in this chapter, we used a methodName_Result( ) callback handler to handle the response of a remote method invocation. No doubt, you noticed the methodName_Status( ) function defined in most of the examples, which is called instead of methodName_Result( ) when an error occurs. In that case, the methodName_Status( ) function receives an error object containing information about the exception that was thrown by the server.

The error object has several properties, as shown in this output from the NetConnection debugger:

Status (object #2)
.....code: "SERVER.PROCESSING"
.....description: "this is a custom exception"
.....details: "   at FlashGateway.Delegates.ASPAdapter.InvokeService(ActionContext 
action)
   at FlashGateway.Delegates.ServiceCommander.InvokeAdapter(ActionContext
      flashContext)
   at FlashGateway.Delegates.ServiceFilter.preInvoke(ActionContext flashContext)"
.....level: "error"
.....type: "System.Exception"

The properties of interest include the description and the type. You can use the description property to display an error for debugging purposes or send yourself an email with that error (through Flash Remoting, of course). The type property is useful when the possibility exists of several different types of exceptions being thrown. See Chapter 4 for more information on the error object and exception handling.

Here is an example of how to handle a simple exception in your ActionScript code:

serviceObject.divideIt (365, 0);

divideIt_Result (result) {
  lblMessage.text = "The result is " + result;
}

divideIt_Status (error) {
  lblMessage.text = "Error: " + error.description;
}

You can also easily check for several different types of exceptions:

divideIt_Status (error) {
  var tmpMsg = "";
  switch (error.type) {
    case "System.DivideByZeroException":
      tmpMsg = "Error: your input must not be a zero";
      break;
    case "System.Exception":
      tmpMsg = "Error: " + error.description;
      break;
  }

  lblMessage.text = tmpMsg;
}

As you can see, you can easily build a robust error-handling system into your Flash application. This is especially true if you spend a bit of extra time at the beginning of your project planning out where you can apply error handling. With a bit of practice, you'll be a pro.



    Part III: Advanced Flash Remoting
     
    ASPTreeView.com
     
    Evaluation has ШЅіїexpired.
    Info...