Handling errors

Describe, Modify, GetProperty, and SetProperty each throw exceptions if an invalid property value is supplied as an argument. Describe and GetProperty throw a System.ArgumentException if the string does not represent a valid property. Modify and SetProperty throw a MethodFailureException whose Message property contains the location of the error in the property value string. For example, consider the following code, where you want to change the color of the id column:

Try
   Dim ModString As String
   ModString = "id.color='255~tif (getrow() < 10,"
   ModString += "RGB(255,0,0, RGB(0, 255,0))'"
   dw1.Modify(ModString)
Catch ex As Sybase.DataWindow.MethodFailureException
   MsgBox("Syntax Error: " + ex.Message)
End Try

The closing parenthesis of the first RGB expression is missing. This generates an exception and displays a message box with the following text:

Syntax error: Modify did not complete successfully; the error message is: Line 1 Column 63: incorrect syntax

Column 63 is actually the end of the supplied string value, which is where the omission of the parenthesis is first detected. If the id column specified in the string is not valid, the incorrect syntax message shows column 9, because the incorrect syntax is detected after the equals sign: id.color=.

When you set an expression using the property values of the GraphicObject classes and assign an invalid property value, the DataWindow server throws a Sybase.DataWindow.InvalidExpressionException, not a MethodFailureException. For example, this code contains the same error as the previous example:

Try
   label = CType(dw1.GetObjectByName("id"),
      Sybase.DataWindow.GraphicObjectText)
   label.TextColorExpression = "if (getrow() < 10,
      RGB(255,0,0, RGB(0, 255,0))"
Catch ex As    Sybase.DataWindow.InvalidExpressionException
   MsgBox("Syntax Error: " + ex.Message,
     MsgBoxStyle.Critical, "Property Assignment Error")
End Try

This produces a message box with the following error text:

Syntax Error: COLOR had an invalid expression. Expression is: if (getrow() < 10, RGB(255,0,0, RGB(0,255,0))

Since the default properties on GraphicObject classes are strongly typed—for instance, the TextColor property is a System.Drawing.Color value—most, if not all, errors in setting these values are detected by the compiler at build time.