Reputation: 2047
what is the best way to handle invalid functions calls with not specified arguments for functions which do not have User interface access
function safeSQRT ( x : Real) : real;
begin
/// check for valid params .....
if (x<0) then
begin
exit;
/// create a exception here ??
end;
end;
Upvotes: 0
Views: 196
Reputation: 613013
With this signature
function safeSQRT(x : Real): Real;
you have but two options to signal an error:
The third option is to change the signature
function safeSQRT(x : Real; out retval: Real): Integer;
where the function return value is now an error code.
If you return an error code you force the caller to explictly check every single return value of each call. That's a heavy burden. Think of the fun involved in calling even the most mundane Win32 API to picture the burden.
If you return a sentinel value, then the same burden is placed on each and every caller.
If you raise an exception then the caller's job is easier. The caller can usually ignore exceptions and let them bubble upwards and be handled be a central exception handler.
The flip side is when the error condition occurs commonly and needs immediate handling. In such scenarios raising an exception is usually the wrong choice. If the error must always be handled at the call site then using exceptions makes it likely that the caller will forget to handle it.
Upvotes: 6