Board index » cppbuilder » How to handle problems in a header file.

How to handle problems in a header file.


2005-10-01 08:52:18 AM
cppbuilder51
I have written a matrix class that is now happily compiling, but there is a certain issue in my mind that is lingering. How should I handle the following problem:
Matrix is sent by user through a call from user code to a matrix inversion routine in header file. Header file code checks and discovers that matrix is not invertible. I have at least a few options.
1. Issue an error statement and return the matrix that was sent.
2. Issue an error statement and return the partially inverted matrix.
3. Issue an error statement and return a completely different matrix, such as a ones or zeroes matrix, although this will probably cause the user's code to experience another error.
4. Program the header file to exit after experiencing this error.
I have a second level of inquiry. If I am writing a header file for general purposes, how do I send an error to the screen without being sensitive to whether the user is using Borland C++ 4.51, VC++, or Borland Builder? If this is a DOS application that is calling the header, would I employ a "printf" or "cout" statement or a "msgbox" in the header file? I realize that the "msgbox" is not an option for a DOS app, so there must be a way to either detect the OS and use some sort of switch, or use a generic message sending routine that requires a confirmation by the user, such as "okay", as in, "okay, I know that a problem occurred, and will not believe anything that my code produces as subsequent results".
I would very much appreciate some expert insight into how to handle these two, related questions. I have to return some matrix, or the compiler isn't happy. I need to send a message describing an error, or the user might be inclined to believe the results. I would be tempted to exit to insure that no erroneous results based upon a partially inverted (and uninvertable) matrix are misinterpreted as being valid by the user, but such an option is not usually considered suitable among Windows programmers.
Thank you in advance for any advice that you might offer.
Sincerely,
DJV
 
 

Re:How to handle problems in a header file.

"DJV" < XXXX@XXXXX.COM >wrote:
Quote

I have written a matrix class that is now happily compiling, but there is a certain issue in my mind that is lingering. How should I handle the following problem:
I assume you mean a library. The header file says what's in the library,
but the library is where the code is.
Quote
Matrix is sent by user through a call from user code to a matrix inversion routine in header file. Header file code checks and discovers that matrix is not invertible. I have at least a few options.

1. Issue an error statement and return the matrix that was sent.
2. Issue an error statement and return the partially inverted matrix.
3. Issue an error statement and return a completely different matrix, such as a ones or zeroes matrix, although this will probably cause the user's code to experience another error.
4. Program the header file to exit after experiencing this error.
5. None of the above.
(None of the above would be acceptable in any library that I would use.)
The solution to this is for the user's code to decide what to do. Either
return an error code, or throw an exception. An error code may be
ignored - an exception certainly can't be, and if the user's code
doesn't deal with it, it then turns into option 4. But the user has the
choice.
Alan Bellingham
--
Me <url:mailto: XXXX@XXXXX.COM ><url:www.doughnut.demon.co.uk/>
ACCU - C, C++ and Java programming <url:accu.org/>
The 2004 Discworld Convention <url:dwcon.org/>
 

Re:How to handle problems in a header file.

"Alan Bellingham" < XXXX@XXXXX.COM >wrote in message
Quote
I assume you mean a library. The header file says what's
in the library, but the library is where the code is.
The code can be inlined inside the header file directly.
Gambit
 

{smallsort}

Re:How to handle problems in a header file.

"DJV" < XXXX@XXXXX.COM >wrote in message
Quote
I have at least a few options.
5. throw an exception that the user's code can catch and handle as desired.
Quote
2. Issue an error statement and return the partially inverted matrix.
It is not good to return partial data. If the user ignores the error
condition, the processing of the partial data could cause other problems
elsewhere.
Quote
3. Issue an error statement and return a completely different matrix,
such as a ones or zeroes matrix, although this will probably cause
the user's code to experience another error.
6. Have the routine accept a matrix as an input parameter, passed by
reference or pointer, that the routine fills in. Don't have the routine
return a matrix only. This way, the routine can then return a separate
boolean or integer indicating success/failure that the user's code can check
if desired.
Quote
If this is a DOS application that is calling the header, would I employ
a "printf" or "cout" statement or a "msgbox" in the header file?
printf() or std::cout will both work just fine. As you noted, there is no
"msgbox" functionality for a DOS application.
Quote
I realize that the "msgbox" is not an option for a DOS app, so there must
be a way to either detect the OS and use some sort of switch, or use a
generic message sending routine that requires a confirmation by the user,
such as "okay", as in, "okay, I know that a problem occurred, and will not
believe anything that my code produces as subsequent results".
Another option is to store the error information in memory somewhere and
then provide some kind of access to it for the user. This way, the user's
code can decide how best to display the error, if at all, when a routine
fails.
Quote
I have to return some matrix, or the compiler isn't happy.
If you use a reference/pointer parameter instead of a return value, you can
get around that, as well as provide the user's code with a separate return
value rather than trying to make the matrix itself appear to be erroneous.
Gambit
 

Re:How to handle problems in a header file.

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Alan Bellingham" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>I assume you mean a library. The header file says what's
>in the library, but the library is where the code is.

The code can be inlined inside the header file directly.

Inline a matrix inversion?!
--
Bruce
 

Re:How to handle problems in a header file.

Thank you, TeamB. I am considering switching from sending a matrix object and returning one to sending a structure containing a matrix and an error message, and returning the same.
Let's see, header file...hmmmm. That's a file that contains additional routines incorporated into one's program by a specific reference to it that notifies the compiler to search for and add the relevant code from that file when constructing an executable. Library, that might be confused with a DLL, which is not the area in which my question focused. I believe most people know what one is referring to when one states that one is using a header file. They don't need to reminded that it contains multiple subroutines or functions. I am grateful for having been provided with a useful response by TeamB.
"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

"DJV" < XXXX@XXXXX.COM >wrote in message
news:433dddc2$ XXXX@XXXXX.COM ...

>I have at least a few options.

5. throw an exception that the user's code can catch and handle as desired.

>2. Issue an error statement and return the partially inverted matrix.

It is not good to return partial data. If the user ignores the error
condition, the processing of the partial data could cause other problems
elsewhere.

>3. Issue an error statement and return a completely different matrix,
>such as a ones or zeroes matrix, although this will probably cause
>the user's code to experience another error.

6. Have the routine accept a matrix as an input parameter, passed by
reference or pointer, that the routine fills in. Don't have the routine
return a matrix only. This way, the routine can then return a separate
boolean or integer indicating success/failure that the user's code can check
if desired.

>If this is a DOS application that is calling the header, would I employ
>a "printf" or "cout" statement or a "msgbox" in the header file?

printf() or std::cout will both work just fine. As you noted, there is no
"msgbox" functionality for a DOS application.

>I realize that the "msgbox" is not an option for a DOS app, so there must
>be a way to either detect the OS and use some sort of switch, or use a
>generic message sending routine that requires a confirmation by the user,
>such as "okay", as in, "okay, I know that a problem occurred, and will not
>believe anything that my code produces as subsequent results".

Another option is to store the error information in memory somewhere and
then provide some kind of access to it for the user. This way, the user's
code can decide how best to display the error, if at all, when a routine
fails.

>I have to return some matrix, or the compiler isn't happy.

If you use a reference/pointer parameter instead of a return value, you can
get around that, as well as provide the user's code with a separate return
value rather than trying to make the matrix itself appear to be erroneous.


Gambit