Jump to content

Exceptions, Pascal vs JS


Recommended Posts

Maybe i asked this before, sorry in that case.

 

When i use the keyword 

raise

the compiler will only accept an "Exception object". It seems this object is magically defined by the compiler (?).

 

My problem is that such an exception object is too "Pascalish". The JS VM (or whoever) can not show me anything useful, just "Object".

 

If you run code like "CL", well it's fine because loops are simulated and there's somewhere to catch the exceptions. But if you want to write "pure" JS stuff, this is not possible.

 

Is there any way to raise for example a string (without using an asm section)?

 

TIA,

 

/Dany

Link to post
Share on other sites

This is a workaround, but i'm not sure it works in all browsers/platforms.

 

procedure TCustomApplication.HandleUncaughtExceptions(evt: JEvent);
var
  lErrEvt := JErrorEvent(evt);
begin
  var lPascalMessage: String;
  if (TVariant.AsObject(lErrEvt.error) is Exception) then
    lPascalMessage := Exception(TVariant.AsObject(lErrEvt.error)).message;

  DisplayError(
    Format('%s %s %s %s: %s',
           [lErrEvt.message, lErrEvt.filename, lErrEvt.lineno, lErrEvt.colno,
           lPascalMessage])
  );

  evt.preventDefault; // Supresses console output, optional.
end;
Depending on what i put into DisplayError i can alert the user or send it MadExcept / Eurekalog - ish.

 

HTH,

 

/D

Link to post
Share on other sites
  • 3 weeks later...
  • 4 weeks later...

Oh, and so that y'all can avoid some pitfall; using that method of adding the event should only be done once. If done twice you'll end up processing it twice - a potential problem. If you have the need to remove the event you can define a delegate (i hope my nomenclature is correct) to make it work:

var errEvt: TEventListener;
errEvt := HandleUncaughtExceptions;

...

Window.addEventListener('error', @errEvt);

...

Window.removeEventListener('error', @errEvt);

HTH,

 

/Dany

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...