The PowerBuilder to .NET compiler changes the exception hierarchy used by the native PowerScript compiler. In the native PowerBuilder environment, Throwable is the root datatype for all user-defined exception and system error types. Two other system object types, RuntimeError and Exception, inherit directly from Throwable.
In the .NET environment, System.Exception is the root datatype. The PowerBuilder to .NET compiler redefines the Throwable object type as a subtype of the System.Exception class, and maps the .NET System.IndexOutOfRangeException class to the PowerBuilder RuntimeError object type with the error message “Array boundary exceeded.” The PowerBuilder to .NET compiler also maps the following .NET exceptions to PowerBuilder error objects:
System.NullReferenceException class to the NullObjectError object type
System.DivideByZeroException class to the DivideByZeroError object type
Figure 13-1 shows the exception hierarchy for PowerBuilder applications in the .NET environment.
Figure 13-1: Exception hierarchy for PowerBuilder in the .NET environment
Even though a .NET exception class is mapped to a PowerBuilder object type, you must use the PowerBuilder object type in your PowerScript code. For example, suppose you define a .NET test class to test for division by zero errors as follows:
public class Test
{
public int division_test (int a)
{
return a/0;
//pops a System.DivideByZero exception
}
}
To catch the error in PowerScript, you can use the DivideByZeroError object type or either of its ancestors, RuntimeError or Throwable. The following PowerScript code catches the error caused by the call to the .NET Test class method for invoking division by zero errors:
int i = 10
string ls_error
try
#IF Defined PBDOTNET Then
Test t = create Test
i = t.division_test(i)
#END IF
catch (DivideByZeroError e)
//the following lines would also work:
//catch (RuntimeError e)
//catch (Throwable e)
ls_error = e.getMessage ( )
end try
Suppose the .NET Test class is modified to catch a custom .NET exception:
public class Test
{
public int second_test (int a)
{
a = a/2;
throw new MyUserException();
}
}
Because MyUserException is a user-defined exception in the .NET environment, it cannot be caught by either the PowerBuilder Exception or Throwable object types. It must be handled inside a .NET conditional compilation block:
int i = 10
string ls_error
#IF Defined PBDOTNET Then
try
Test t = create Test
i = t.second_test
catch (MyUserException e)
//this will also work: catch (System.Exception e)
ls_error = e.getMessage()
end try
#END IF