Top  Previous  Next

What is translated > class-reference type > ExceptionRef

There is a special problem with exceptions: while Delphi exceptions as all other classes are derived from TObject, this isn't possible in C#. In C# only classes derived from System.Exception can be thrown and catched. Therefore ClassRef''s, which are made to create TObject types cannot be used for exceptions. Delphi2C# uses an analogously ExceptionRef<T> instead. The definition of this class is inserted on top of the manually prepared System.External.ExcUtils.cs. Like ClassRef<T> ExceptionRef<T> is derived from TMetaClass, but instead of the function


public override TObject Create()


ExceptionRef<T> has the function:


public override System.Exception Create(string s)


This function uses the CreateInstance function from System.Reflection to create exceptions. The base function:


public virtual Exception Create(string s)


has been added to TMetaClass for this reason.


By use of the ExceptionRef<T> hierarchy for example the following line of code in System.Sysutils:


     E := ExceptTypes[ExceptMap[ErrorCode].EClass].Create(ExceptMap[ErrorCode].EIdent);


is translated to C# quite natuaral to:


  E = (System.SysUtils.Exception) ExceptTypes[(int) ExceptMap[ErrorCode - 3].EClass].Create(ExceptMap[ErrorCode - 3].EIdent);



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content