Food for thought - language string improvement

classic Classic list List threaded Threaded
47 messages Options
123
Reply | Threaded
Open this post in threaded view
|

Re: Food for thought - language string improvement

Graeme Geldenhuys-6
On 2017-07-10 14:46, Graeme Geldenhuys wrote:
> You
> can also have a single catch block with a comma separated list of
> exception types.

Oops, typo.... Not a comma separated list, but a list separated by the |
(pipe or vertical bar) symbol.

See the last example here:

   https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html


Regards,
   Graeme

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Food for thought - language string improvement

Felipe Monteiro de Carvalho
In reply to this post by Graeme Geldenhuys-6
On Mon, Jul 10, 2017 at 3:46 PM, Graeme Geldenhuys
<[hidden email]> wrote:
> Then lastly, you can even add a finally block in there too (Java calls this
> try-with-resources).

No, that's not what try-with-resources is.

This is try-with-resources:
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

Any object which implements the interface AutoClosable is auto-closed
in the try, regardless of an exception happening or not.

Pascal is a little different, since it has destructors, so I guess in
Pascal we could auto-free objects in a hypotetical try-with-resources
in Pascal. Something like:

try ( stringList := TStringList.Create;
 otherObj := TSomeOtherClass.Create; )
  code here
except
  exception block, optional
end;

And any object in the () section would be auto-freed, auto-checking if
it was already created or not.

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Food for thought - language string improvement

dmitry boyarintsev
In reply to this post by Graeme Geldenhuys-6


On Mon, Jul 10, 2017 at 9:46 AM, Graeme Geldenhuys <[hidden email]> wrote:
On 2017-07-10 13:34, Dmitry Boyarintsev wrote:
are you referring to "Catching More Than One Type of Exception with One
Exception Handler" in
https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html

Yes. You can have multiple catch blocks inside the same try block.

Well, this has been in FPC/Delphi from the start
Ultimately handling multiple exceptions in one catch block is the whole purpose of having exception mechanism in a language.


You can also have a single catch block with a comma separated list of exception types.

I think it's actually Pipe separated.
And is that true, that the variable specified for catching such exception would default to a common "Exception" class?
Not available in freepascal indeed.

 
Then lastly, you can even add a finally block in there too 
Also not available, but people are asking : 



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Food for thought - language string improvement

Free Pascal - General mailing list
In reply to this post by Graeme Geldenhuys-6

Am 10.07.2017 15:46 schrieb "Graeme Geldenhuys" <[hidden email]>:
>
> On 2017-07-10 13:34, Dmitry Boyarintsev wrote:
>>
>> are you referring to "Catching More Than One Type of Exception with One
>> Exception Handler" in
>> https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
>
>
> Yes. You can have multiple catch blocks inside the same try block. You can also have a single catch block with a comma separated list of exception types.

You can catch multiple exception types in Object Pascal as well:

=== code begin ===

try
expect
  on e: EWhatever do ...;
  on e: EFoobar do ...;
  else ...
end;

=== code end ===

Though you can't use the same code block for multiple types.

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Food for thought - language string improvement

dmitry boyarintsev
On Mon, Jul 10, 2017 at 11:19 AM, Sven Barth via fpc-pascal <[hidden email]> wrote:

Though you can't use the same code block for multiple types.

Structured development to the rescue :) 

===
program project1;

{$mode delphi}
{$RANGECHECKS on}

uses SysUtils;

function HandlingIt(const e: exception): Integer;
begin
  writeln('something bad has happened:');
  writeln(e.message);
  Result:=0;
end;

function Catch(a,b: byte; oper: char): byte;
begin
  try
    if oper = '+' then Result:=a+b
    else if oper = '/' then Result:=a div b;
  except
    on e: EDivByZero do begin
      writeln('division by zero');
      Result:=0;
    end;
    on e: EIntOverflow do Result:=HandlingIt(e);
    on e: ERangeError do Result:=HandlingIt(e);
  end;
end;

begin
  writeln( catch (1,1,'+'));
  writeln( catch (255,255,'+'));
  writeln( catch (1,0,'/'));
end.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Food for thought - language string improvement

noreply
In reply to this post by Ched
On 2017-07-08 08:29, Ched wrote:

> Is this acceptable ?
>
>> 'SELECT Customers.CustomerName, Orders.OrderID' +
>> 'FROM Customers' +
>> 'FULL OUTER JOIN Orders' +
>> 'ON Customers.CustomerID = Orders.CustomerID' +
>> 'ORDER BY Customers.CustomerName;'
>
> I think that one have to insert space at some loactions:
> 'SELECT Customers.CustomerName, Orders.OrderID' +
> ' FROM Customers' +
> ' FULL OUTER JOIN Orders' +
> ' ON Customers.CustomerID = Orders.CustomerID' +
> ' ORDER BY Customers.CustomerName;';
>
> Cheers, Ched
>

And if it is a multiline string, the compiler may have to convert
carriage return/new line into a space for you, to allow it to take:

FROM Customers
FULL OUTER JOIN Orders

and convert the carriage return after "Customers" into a space for you..

otherwise it will be:

FROM CustomersFULL OUTER JOIN Orders

..concatenated without a space

Something to be concerned about if implementing it.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Food for thought - language string improvement

Florian Klämpfl
In reply to this post by Free Pascal - General mailing list
Am 10.07.2017 um 15:00 schrieb Sven Barth via fpc-pascal:

> Am 10.07.2017 13:19 schrieb "Michael Van Canneyt"
> <[hidden email] <mailto:[hidden email]>>:
>>
>>
>>
>> On Mon, 10 Jul 2017, Felipe Monteiro de Carvalho wrote:
>>
>>> On Mon, Jul 10, 2017 at 1:08 PM, Michael Van Canneyt
>>> <[hidden email] <mailto:[hidden email]>> wrote:
>>>>
>>>> The code is definitely not the same. In each case, it was measured.
> There is
>>>> a 10% performance loss.
>>>
>>>
>>> I'd love a source on this one. I guess you mean in Free Pascal?
>>
>>
>> Obviously.
>>
>> The classes unit can be recompiled to use the fgl (well, that used to
> be so)
>> as a basis. When using the resulting list and stringlist, there was a 10%
>> performance loss. The main reason - If I recall correctly - was that
> the fgl needs to resort to move() operations instead of direct assignments.
>
> The fgl classes don't use Move(), but they have a virtual method that
> does the assignment between the specialized parameters that's inherited
> from the non-generic parent list type.

... which is done to avoid bloat. fgl specialized only a wrapper around
an internal list. So the slightly slower code in some cases reduced
executable size.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
123