1

Calling a Delphi side function from a script that raises an exception raises a generic EDelphi exception on the script side. This hides the exception class of the actual Delphi side exception. I tried to investigate the implementation of the EDelphi Ijaddajadda interface but am lacking skills to see a way to extract the Delphi side class or at least the class name. The truth has to be out there...

Example, script side

Try
 MyDelphiSideFunction; // May raise EOutOfmemory
Except on E:Exception do
 PrintLn(E.ClassName);
End;

This always prints "EDelphi", no matter the actual Delphi side exception class (EOutOfmemory in this example).

How to get the "real" class or class name from E?

0

1 Answer 1

4

You cannot directly get the exception class type as the DWScript exception handler doesn't store that information anywhere and the Delphi side type wouldn't make sense inside the script anyway. You can however easily get the class name from the EDelphi script side exception object via the EDelphi.ExceptionClass property. Like this:

Script

try

  var x := 1/0;

except
  on E: EDelphi do
    WriteLn('Error: ' + E.Message + ' (' + E.ExceptionClass + ')');

  on E: Exception do
    WriteLn('Error: ' + E.Message);
else
  WriteLn('Unknown exception type');
end;

Output

Error: Floating point division by zero (EZeroDivide)

You can see the DWScript source code that transforms a Delphi side exception to a script side EDelphi exception here: https://github.com/EricGrange/DWScript/blob/2d61c8a95fb4a2aab328f3cc84bb9f243c927286/Source/dwsExprs.pas#L2244

How I found the answer

I simply searched the source on Github (the search on the main Bitbucket repo sucks) for "EDelphi": https://github.com/search?q=repo%3AEricGrange%2FDWScript%20EDelphi&type=code

This gave me this piece of code:

// A Delphi exception. Transform it to a EDelphi-dws exception
exceptObj:=CreateEDelphiObj(mainException.ClassName, mainException.Message);

I then looked at the CreateEDelphiObj method:

function TdwsProgramExecution.CreateEDelphiObj(const ClassName : String;
                                   const Message : String) : IScriptObj;
begin
   Result := IScriptObj(IUnknown(
      ProgramInfo.Vars[SYS_EDELPHI].Method[SYS_TOBJECT_CREATE].Call([ClassName, Message]).Value));
   (Result.ExternalObject as TdwsExceptionContext).ReplaceTop(LastScriptError); // temporary constructor expression
end;

This looks like a call to a script side constructor, passing the ClassName and Message strings as parameters. That's fine, but what happens to the ClassName value?

Again, looking at the search result I noticed the Delphi side implementation of the EDelphi constructor:

procedure TDelphiExceptionCreateMethod.Execute(info : TProgramInfo; var ExternalObject: TObject);
begin
   inherited;
   Info.ValueAsVariant[SYS_EDELPHI_EXCEPTIONCLASS_FIELD]:=Info.ValueAsVariant['Cls']
end;

So the constructor calls the inherited constructor and then stores the ClassName value in the FExceptionClass (the value of the SYS_EDELPHI_EXCEPTIONCLASS_FIELD constant) field.

At this point I could guess that the property would be called ExceptionClass so I used my DWScriptStudio IDE/debugger to write the example and verify the result. DWScriptStudio

4
  • Many thanks @AndersMelander, this is exactly what I was searching for. But where on earth did you find the ExceptionClass property? I crawled through the DWS sources for hours for something like this. And even a full grep for ExceptionClass over all DWS sources did not reveal a meaningful hint to me. Would you mind to enlighten me? Fun fact: DWS is perfectly happy with var x := 1/0, resolving for INF. Can this be changed to raise an exception instead?
    – LuWey
    Sep 23 at 15:55
  • I've updated my answer to explain how I found the ExceptionClass property. With regard to INF vs. an exception, it could be a difference in how our exception masks are set up, or it could be this one: stackoverflow.com/questions/47611635/… Sep 23 at 19:33
  • Wunderful explanation @AndersMelander. Really appreciate! And your DWScript Studio also is a great piece of work I'm currently having a look at. Am wondering how hard it would be to strip out that costly DevExpress.
    – LuWey
    Sep 24 at 19:02
  • Most of the DevExpress controls have corresponding equivalent VCL controls but a few don't (e.g the TcxTreeList). The ribbon can easily be replaced with something else since all the UI logic is delegated to actions. I think the hardest part will be the docking system. The VCL docking system is... problematic, at best. Sep 25 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.