Recent

Author Topic: Exception or error flag?  (Read 4125 times)

glorfin

  • Full Member
  • ***
  • Posts: 148
  • LMath supporter
Exception or error flag?
« on: November 21, 2017, 12:42:56 am »
Hi all!
I am working on a scientific/mathematical library for FPC.
There is a choice what to do if an error or indefinite situation occurs, like invalid arguments passed, no roots of equation exist etc.
One possibility is to have an error flag variable (Something like SciError, and set SciOK when everything was OK or any other value to flag an error); other possibility is to raise an exception.

The library is organized as a set of directly linkable packages, not dll, so problem of exception passing from dll to a calling program is not an issue.

What would be your suggestions?

Thanks in advance!

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Exception or error flag?
« Reply #1 on: November 21, 2017, 02:43:31 am »
It's best to use the standard   TRY, Raise, Exception , Finally etc..

These of course are for cases where errors should not be generated unless due to a
coder error..

 Most coders assumes  any function that receives parameters from a dynamic/Dereference
value that may cause an issue, like divide by 0 for example, is usually encapsulated with a Try and except
statement to catch possible Raise exceptions in functions, for example, a math function you may create and
force it to fault.

 There are times where a complex return type maybe required and thus employment of a record is needed.
In such cases, you can have a Boolean in there to indicate pass or failure.

Btw, Is the Math unit insufficient for your needs?

 I nice DFT and IDFT , FFT would be nice as a stand alone LIB ;)
The only true wisdom is knowing you know nothing

Leledumbo

  • Hero Member
  • *****
  • Posts: 8744
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Exception or error flag?
« Reply #2 on: November 21, 2017, 05:21:38 am »
Both are fine, so long as it is consistent. The benefit of exception is that error handling is made separate from normal flow, with some performance penalty.

glorfin

  • Full Member
  • ***
  • Posts: 148
  • LMath supporter
Re: Exception or error flag?
« Reply #3 on: November 21, 2017, 02:16:41 pm »
Thank you for your reply, Jamie!
It's best to use the standard   TRY, Raise, Exception , Finally etc..

Well, try... except... finally will be job of a user of the library; as an author of it I will use only Raise if
I opt for exceptions mechanism.

Quote
Most coders assumes  any function that receives parameters from a dynamic/Dereference
value that may cause an issue, like divide by 0 for example, is usually encapsulated with a Try and except
statement to catch possible Raise exceptions in functions, for example, a math function you may create and
force it to fault.

There are times where a complex return type maybe required and thus employment of a record is needed.
In such cases, you can have a Boolean in there to indicate pass or failure.
So, your point of view is that exception is not a best mechanism for situation when an error is generated due to
invalid user-supplied values, right?

Mechanism with the return of boolean as an error or success flag I do not like very much, because in many cases
it is convenient to return a result result of computations and, as Leledumbo rightly points out, error handling must
be uniform throughout the library.

Quote
Btw, Is the Math unit insufficient for your needs?
Yes  :)
Quote
I nice DFT and IDFT , FFT would be nice as a stand alone LIB ;)
Well, signal processing. For now, in his field I have non-visual components for
gaussian, moving average and median filters.

glorfin

  • Full Member
  • ***
  • Posts: 148
  • LMath supporter
Re: Exception or error flag?
« Reply #4 on: November 21, 2017, 02:21:51 pm »
Thank you for your reply, Leledumbo!
Both are fine, so long as it is consistent.
Well, this is exactly what makes the choice difficult  :)

Disadvantage of error variable is that it is easily overlooked. Calling programmer forgets
to check the variable, user gets wrong output because the function failed, but nobody
knows that, and there are cases when a wrong output does not look obviously wrong.

From the other hand, unhandled exception can crush a program, which may be too cruel to
end user.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Exception or error flag?
« Reply #5 on: November 21, 2017, 04:36:07 pm »
One approach is to code all your routines as boolean functions (which may then need var or out parameters, and an errMsg parameter).
If the function returns false you know immediately there is an error and that the resulting parameters are invalid.

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: Exception or error flag?
« Reply #6 on: November 21, 2017, 06:05:18 pm »
One possibility is to have an error flag variable (Something like SciError, and set SciOK when everything was OK or any other value to flag an error); other possibility is to raise an exception.
What would be your suggestions?
If the user of your functions expects an error, or if such behavior is possible, then it is better to use the error flag or boolean. For example: FindFirst, FindNext. But if no errors are expected, then it is better to use the raise expression.

Nitorami

  • Sr. Member
  • ****
  • Posts: 481
Re: Exception or error flag?
« Reply #7 on: November 21, 2017, 06:44:18 pm »
As this is a topic not specific to fpc, you may also consider C++ guidelines useful, such as

http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-errors

Leledumbo

  • Hero Member
  • *****
  • Posts: 8744
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Exception or error flag?
« Reply #8 on: November 22, 2017, 08:04:53 am »
Thank you for your reply, Leledumbo!
Both are fine, so long as it is consistent.
Well, this is exactly what makes the choice difficult  :)

Disadvantage of error variable is that it is easily overlooked. Calling programmer forgets
to check the variable, user gets wrong output because the function failed, but nobody
knows that, and there are cases when a wrong output does not look obviously wrong.

From the other hand, unhandled exception can crush a program, which may be too cruel to
end user.
You can actually install a global unhandled exception handler that would never crash any program that uses your unit, provided the handler itself is proven to be safe and doesn't conflict with other unit. Take a look at System.ExceptProc variable.

glorfin

  • Full Member
  • ***
  • Posts: 148
  • LMath supporter
Re: Exception or error flag?
« Reply #9 on: November 22, 2017, 10:38:26 pm »
Yes, thank you for this third possibility  :)
One approach is to code all your routines as boolean functions (which may then need var or out parameters, and an errMsg parameter).
If the function returns false you know immediately there is an error and that the resulting parameters are invalid.

 

TinyPortal © 2005-2018