[fpc-pascal] Re: using exceptions
Lukasz Sokol
el.es.cr at gmail.com
Wed Jan 4 13:18:24 CET 2012
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...
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)
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.
L.
More information about the fpc-pascal
mailing list