[fpc-pascal] Re: using exceptions
Marcos Douglas
md at delfire.net
Wed Jan 4 13:47:00 CET 2012
On Wed, Jan 4, 2012 at 9:18 AM, Lukasz Sokol <el.es.cr at gmail.com> wrote:
> On 04/01/2012 11:50, Marcos Douglas wrote:
>
>> Ok, I expressed myself poorly.
>> When I said: "If we could return more than one value, ok we (maybe) do
>> not need Exceptions."
>> I mean: "If we could return more than one value, using a better way
>> than 'out arguments' or 'array of const', ok we (maybe) do not need
>> Exceptions in many times".
>>
>> I know we can use 'out args', 'array' or a object (this provides many
>> results) but this is not beautiful.
>> 1- out args: we always have a local variable ErrorCode/ErrorMsg (like
>> your example) and this is boring.
>> 2- array of const: we need 'unpack' the array; memleak could happen; etc;
>> 3- object: we have a local variabel or use 'with' but memleak could
>> happen any way;
>>
>> Marcos Douglas
>
> You'd still need exceptions code to protect yourself from corner cases
> (like data-(stream) dependent calculation that could possibly hit float div/0 for example)
> because if you don't, your app will bail out...
Correct, because the language was designed that way.
> otherwise e.g. your maths functions instead of /just/ doing what they need, will
> have to check for possible misformed argument(s); and (IIIC - if I imagine correctly)
> the exception catching code is closely paired to arithmetic errors like these;
> also out-of-memory errors (may be ?) (which could explain the recommended everywhere
> try-finally-end use case for most locally declared objects of e.g. TStringList type)
Correct and try-finally is indispensable.
> Object Pascal isn't 'C-plain'like where you'd need manual check for such cases and commonly
> use 'goto error_label;' (common usage in Linux kernel AFAIS, go figure.)
>
> C++ and Java also have underlying exception handling code.
>
> IM(H)O you can't have a function return both a value (of whatever kind) and an error code without
> seriously redefining language syntax.
> Thing is, (most?) programming languages are based on math concept of a 'function' which is supposed
> to return one meaningful value, where it's up to the upper layer to check for correctness of
> arguments... either that or you check arguments and /abort/ function execution if they're bad.
> And if the function return value is allowed -inf..0..+inf? you definitively need another out argument
> to signal the upper layer something went wrong.
>
> I see exception handling code/classes/objects as a globally (thread-safe?) accessible way of bailing out
> from bad situation and telling the upper layer about it. It is effectively your way of 'returning
> another value from function', if you look at it from a distance.
I agree with you! But I was discussing the possibility do not using
Exceptions. ;-)
Another point is when and where uses Exceptions... that's what they
were discussing.
Marcos Douglas
More information about the fpc-pascal
mailing list