Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 4 of 4 total
Thread Exception cost
Mon, May 9 2016 10:14 AMPermanent Link

Matthew Jones

How expensive are exceptions in EWB JavaScript?

I have a validation routine that validates user input to check it is a
valid percentage, float, or currency value. As part of the validation,
it needs to strip things like percent signs, and so it makes sense for
it to calculate the value too. However, with no var parameters, I can
only return true or false for valid or not. Thus I'm currently using a
"GetLastValue" function that returns a value stored in a global by the
validator.

procedure TfrmDetailEdit.editQtyChange(Sender: TObject);
var
   szText : String;

   bValid : Boolean;
begin               
   if m_bLoading then
       exit;

   szText := editQty.Text;
   bValid := IsValidDEC(szText);
   if bValid then
   begin
       m_xItemMaterial.QuantityDEC := LastValidDEC;
       UpdateFromLine;
   end;
   editQty.Error := not bValid;
end;

But it struck me that it might be better to encapsulate it all with a
function that assumes all is well, but throws an exception if it was
bad:

procedure TfrmDetailEdit.editQtyChange(Sender: TObject);
begin               
   if m_bLoading then
       exit;

   try
     m_xItemMaterial.QuantityDEC := IsValidDEC(editQty);
     UpdateFromLine;
   except
   end;
end;

I'd pass in the edit field, so it could set the valid/invalid state,
get the text, and return the validated value. If there was a problem,
it would raise the exception, and the code would be inert. But how
expensive are exceptions?


--

Matthew Jones
Mon, May 9 2016 11:24 AMPermanent Link

Raul

Team Elevate Team Elevate

On 5/9/2016 10:14 AM, Matthew Jones wrote:
> How expensive are exceptions in EWB JavaScript?

I'm no expert but looks like you're doing UI code so i would say don't
worry about it since exception cost would not be even in the same cost
factor universe as UI and user input timings.


Raul
Mon, May 9 2016 11:50 AMPermanent Link

Matthew Jones

Raul wrote:

>  looks like you're doing UI code so i would say don't worry about it
> since exception cost would not be even in the same cost factor
> universe as UI and user input timings.

Indeed, I was thinking that, but it is possible that these things will
ripple through, so I'd like to know if avoiding a cascade is important
or not. It is possible that I have to do a few thousand calculations
for an update at some point (not sure at this point, probably a few
hundred max).

--

Matthew Jones
Mon, May 9 2016 2:51 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< How expensive are exceptions in EWB JavaScript? >>

Chrome's V8 JS engine won't optimize any try..except/finally blocks, so there's that, but I wouldn't worry about it because you won't ever notice the difference.

As for actually *throwing* the exceptions, that could become expensive and should be avoided if you think it's going to be a common occurrence.

Tim Young
Elevate Software
www.elevatesoft.com
Image