Simulation & Probabilistic Analysis SDK
Add Distribution Fitting & Simulation Features to Your Applications

SPA SDK Help HomeLearn More About the SPA SDK

Error Handling

The SPA SDK supports the standard COM error handling mechanism. Many object member functions can return error codes which should be handled by your application. The error handling technique can vary depending on the development platform used - VBA, C#, VB.NET, C++.NET, or unmanaged C++.

Error Handling in Visual Basic for Applications

In VBA, you can use the Err.Number property to extract the error code returned by the SDK:

  Dim data() As Double

  ' set error handler
  On Error GoTo ErrHandler

  Call dist.RandomArray(-100, data)  ' dist has the type SPADistribution 

  Exit Sub

  ' error handler
  ErrHandler:
    If Err.Number = SPAEInvalidDist Then
      MsgBox "Invalid distribution type or parameters"
    ElseIf Err.Number = SPAEInvalidArg Then
      MsgBox "Invalid function arguments"
    Else
      MsgBox "Some other error occured"
    End If  
  

Error Handling in .NET (C#, VB.NET, C++.NET)

In .NET languages, you can use the try/catch block and the standard System.Exception class to obtain the error information. Since the numerical COM error codes are not available through this class, the SDK returns the codes as strings via the Message property. For instance, the typical C# error handling code will be as follows:

  try
  {
    double[] r = null;
    dist.RandomArray(-250, ref r);  // dist has type SPADistribution 
    Console.WriteLine(r.Length);
  }
  catch (Exception e)
  {
    SPAErrorType error = (SPAErrorType)Convert.ToInt32(e.Message);

    switch (error)
    {
      case SPAErrorType.SPAEInvalidDist:
           Console.WriteLine("Invalid distribution type or parameters");              
           break;

      case SPAErrorType.SPAEInvalidArg:
           Console.WriteLine("Invalid function arguments");              
           break;

      default:
           Console.WriteLine("Some other error occured");
           break;
    }
  }
  

Error Handling in C++ (unmanaged)

In native C++, the error information can be retrieved using the try/catch block and the standard _com_error class. To extract the numerical error code, you should convert the return value of the _com_error::Description() member function to integer:

  try
  {
    SAFEARRAY *randomData = NULL;  
    dist->RandomArray(-100, &randomData);  // dist has type ISPADistribution* 
  }
  catch (_com_error &err)
  {
    switch (atol(err.Description()))
    {
      case SPAErrorType::SPAEInvalidDist:
           printf ("Invalid distribution type or parameters\n");
           break;

      case SPAErrorType::SPAEInvalidArg:
           printf ("Invalid function arguments\n");
           break;

      default:
           printf ("Some other error occured\n");
    }
  }
  

Error Codes

The SPAErrorType enumeration includes all the error codes supported by the SDK:

Error code
Numerical value
Description
SPAEGeneric -2147220991 Generic application error.
SPAEInvalidData -2147220990 You are trying to use an uninitialized SPADataSet object (use the SPADataSet.Assign method to specify your data).
SPAEInsufficientDataSize -2147220989 The sample size is too small - it must contain at least five values.
SPAEInvalidDataDomain -2147220988 You have specified the discrete data domain, but the data set contains fractional numbers.
SPAEInvalidDensityData -2147220987 The following criteria is not satisfied: for continuous X-Density input data format, the Density data values must be non-negative, and the sum of these values must be greater than zero.
SPAEInvalidCountData -2147220986 The following criteria is not satisfied: for discrete X-Count input data format, the Count data values must be positive integers.
SPAEConstantData -2147220985 The input data is constant - no statistical analysis can be performed.
SPAEInvalidArg -2147220984 Invalid function arguments supplied.
SPAEAppNotInitialized -2147220983 The SPAApplication.Initialize method hasn't been called before using any functions of the SDK.
SPAEOutput -2147220982 Output error (for example, the graph cannot be saved to a file).
SPAEInvalidDist -2147220981 This error is returned when you are trying to use the SPADistribution object member functions before specifying the distribution type using the SPADistribution.Type property, or the distribution parameters are invalid (see SPADistribution.ParamValid).
SPAEPosInf -2147220980 The return value of a function is a positive infinity.
SPAENegInf -2147220979 The return value of a function is a negative infinity.

Copyright © MathWave Technologies
www.mathwave.com