Re: Error handling
From: Barry Schwarz (schwarzb_at_deloz.net)
Date: 04/23/04
- Previous message: Barry Schwarz: "Re: memory allocation nightmare"
- In reply to: Alberto Giménez: "Error handling"
- Next in thread: Alberto Giménez: "Re: Error handling"
- Reply: Alberto Giménez: "Re: Error handling"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 23 Apr 2004 00:47:29 GMT
On Thu, 22 Apr 2004 17:32:33 +0200, Alberto Giménez
<algibe@teleline.es> wrote:
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>
>Hi there! I'm doing some homework (almost finished), but now i'm
>reconsidering a design decission I made at the beginning. This is about
>error handling in functions. I have my functions returning 0 if OK, and
>an "enum" error indicating the type of error. A typical pseudo-code is
>like:
>
>if (error1)
> return E_NOMEM;
>
>...
>
>if (error2)
> return E_CLOSED;
>
>...
>
>Then, I have some modules calling functions of some others, so I have up
>to 3-deep "own function calling". Each of this "deep level" returns 2 or
>3 kinds of error (E_FOO, E_BAR,...), and I considered to bypass all
>error to topmost calling function or even main program, but the
>problem is that in the main program i should have a code like:
>
>status = myfunc ();
>switch (status) {
> case 0: /* ok */
> ...
> case E_NOMEM:
> ...
> case E_CLOSED:
> ...
>}
>
>and something like this for all my functions returning more than one
>value indicating a diferent error. This way my program can indicate
>more or less exactly what produced an error in the execution.
>
>I've seen that standard C functions use -1 for error, modifying errno,
Which ones do you think use -1 to indicate errors. I know some that
use NULL, some that use EOF, at least one that uses 1, and a few that
use 0, and at least one where -1 is a normal return value. But off
the top of my head I cannot think of one that uses -1.
>but I don't like this handling, because i would have personal error
>types. In the other hand, my error handling and bypassing is not very
>good (I think).
>
>Now I'm considering to just return -1 (or another nonzero value) in all
>functions if there is any error, and the top caller just would indicate
>something like "there was an error in myfunc ()" (with no clue if there
>was a sub-call error inside myfunc).
Instructor preferences vary but when I was teaching I always gave
preference to user friendliness. Will including a function name in an
error message be meaningful to your user?
>
>I'm doing error-bypassing to the caller because I want my code
>modular, and don't want IO functions messing around inside my
>modules (a kind of OOP). For this reason, only the main program should
>write messages to the user.
>
>In the other hand, does C standard say anything about error handling in
>functions?
For those functions which return an error indication, the standard
specifies what that indication is on a function by function basis.
The standard does not specify what your program should do when such a
condition is indicated.
>
>TIA
<<Remove the del for email>>
- Previous message: Barry Schwarz: "Re: memory allocation nightmare"
- In reply to: Alberto Giménez: "Error handling"
- Next in thread: Alberto Giménez: "Re: Error handling"
- Reply: Alberto Giménez: "Re: Error handling"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|