473,788 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error handling

-----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,
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).

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?

TIA

- --
Alberto Giménez, SimManiac en el IRC
http://www.almorranasozial.es.vg
GNU/Linux Debian Woody 3.0 GnuPG ID: 0x3BAABDE1
Linux registered user #290801
Iniciando Windows 98... 2 horas despues.... Iniciando Windows 98...
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFAh+WR0ke CtzuqveERAlP+AJ 9IN+qpwkTJ0TQ3r v2BlQwEB3DESgCe IzjD
SrcKrjKmKCcAjij 0SkECogc=
=PIEb
-----END PGP SIGNATURE-----
Nov 14 '05 #1
3 2661
On Thu, 22 Apr 2004 17:32:33 +0200, Alberto Giménez
<al****@telelin e.es> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi there! I'm doing some homework (almost finished), but now i'm
reconsiderin g 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>>
Nov 14 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

El 23 Apr 2004 00:47:29 GMT, Barry Schwarz escribió:
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.


Hm, well, i think i meant system dependent system calls instead of
standard C functions, but I found "mktime", and rationale says that
should return (time_t) -1 on error (can't figure out how "standard" can
be the rationale itself).

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 don't mean to put the function name, but including what the function
does, as an example, let's imagine a function that creates a socket for
a server to "hear" on it. Let's name that function "create_soc ket" that
function calls to another function called "socket", another called
"bind" and some others. If that "create_soc ket" fails, my 2 ways of
handling the errors are:

1) say the caller which sub-call failed, for example, return E_BIND if
bind fails, or E_SOCK if socket call fails, and the caller print a useful?
message saying something like:
"couldn't create socket" or "couldn't bind server to a port", indicating
that "create_soc ket" failed by the "socket" call or by the "bind" call.

2) don't mind about sub-call errors, and the caller of the
"create_soc ket" function should say something like:
"couldn't create a server socket", indicating a general error on
"create_socket" .

Greetings.

- --
Alberto Giménez, SimManiac en el IRC
http://www.almorranasozial.es.vg
GNU/Linux Debian Woody 3.0 GnuPG ID: 0x3BAABDE1
Linux registered user #290801
Se busca ladron/estafador con buenas referencias para empresa
con gran proyeccion. Preguntar en ca********@tele fonica.es .
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFAiSwo0ke CtzuqveERApeSAJ 0bgKIG5Zvc8LaGB/pNCwgTGVAQ0gCgi oZb
3Iw3/xV8gff96zAL+Lzz G9A=
=Ta65
-----END PGP SIGNATURE-----
Nov 14 '05 #3

"Alberto Giménez" <al****@telelin e.es> wrote in message
I don't mean to put the function name, but including what the
function does, as an example, let's imagine a function that creates a
socket for a server to "hear" on it. Let's name that function
"create_soc ket" that function calls to another function called
"socket", another called "bind" and some others. If that
"create_soc ket" fails, my 2 ways of handling the errors are:

1) say the caller which sub-call failed, for example, return E_BIND > if bind fails, or E_SOCK if socket call fails, and the caller print a useful? message saying something like:
"couldn't create socket" or "couldn't bind server to a port",
indicating that "create_soc ket" failed by the "socket" call or by the
"bind" call.

2) don't mind about sub-call errors, and the caller of the
"create_soc ket" function should say something like:
"couldn't create a server socket", indicating a general error on
"create_socket" .

This really isn't a solved problem. In C++ you should throw exceptions,
which means that high-level code will always catch an error from where it
occurred. However it won't necessarily know the particular function in which
it occurred.

I think you can look at the issue this way. There are three types of error
a) When you can't get an IO resource needed
b) When input is badly formed
c) When the program runs out of memory.

If you separate out IO then you are left with error types b) and c). With a
few exceptions, such as functions that take input to be parsed by a grammar,
if you have a type b) error then that is a mistake by the calling
programmer. In a debug environment you should assert() fail. In a user
environment things are more difficult - basically something has gone wrong
and there is no good solution. You can terminate the program with a "CD
dirty" message if you like.
A type c) error is more a theoretical than a practical problem on modern
systems if you are asking for a small amount of memory. If the system won't
give you 1Kb for a buffer then you can be pretty sure that well before then
it will have generated "memory low" warnings. So it doesn't really matter
what you do, since the error-hnadling code will be executed too infrequently
to make any difference to the user.
If you are allocating a large amount of memory then of course the situation
is different - it's quite possible that the computer will be genuinely out
of memory. So you need to pass an "out of memory" flag up the control stack.
Note that the user, or even debugging programmer, doesn't really need to
know where you ran out of memory. There's nothing wrong with the function
itself, it just doesn't have resources to execute.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3274
by: WSeeger | last post by:
When creating a new class, is it encouraged to always include error handling routines within your LET and GET procedures? It's seems that most text books never seem to include much about error handling within classes. Just hoping to hear some programmer's thoughts on error handling.
12
6696
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that assert could be used to start an interactive debugger automatically. How do I realize that on a Linux machine using gcc?
6
8468
by: Squirrel | last post by:
I have a command button on a subform to delete a record. The only statement in the subroutine is: DoCmd.RunCommand acCmdDeleteRecord The subform's recordsource is "select * from tblVisit order by VisitDt" I'm getting this error message: Errno is 2465. Err.description is "Can't find field '|' referred to in your expression"
13
4486
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently runs into problems on the system where it will actually be used, and since I used so little error-trapping it dies very ungracefully. I will of course try to fix whatever is causing the error and add error-trapping to the functions where the...
21
4426
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So if "global variables get re-set" doesn't tell the whole story, then what does? ***please note*** I'm not looking for a solution - I'm looking for a more detailed description of what happens when an un-handled error occurs - possibly with help file...
3
2859
by: Stefan Johansson | last post by:
Hi all I'am moving from Visual Foxpro and have a question regarding "best practice" error handling in vb .net. In VFP I have always used a "central" error handling object in order to have a easy and reusable way of handling all errors in a program. The VB 6 coding examples I have seen there has always been error handling code in each program module.
4
1939
by: Al Williams | last post by:
Hi, I have error handling in place throughout my application. I also start the application wrapped in error handling code to catch any unexpected exceptions (i.e. exceptions that occur where I haven't placed error handling code). When I run my app from the IDE, the unhandled errors are caught by the error handling code in my Sub Main routine and the error details are logged to a text file and optionally e-mailed to me for follow-up.
10
2296
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have written a sample function to look up an ID in a table. The function returns True if it can find the ID and create a recordset based on that ID, otherwise it returns false. **I am not looking for comments on the usefulness of this function - it is
0
11599
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access Security, but I'll start with something short and simple This code was written in Access 2003 but should be valid in Access 2000 By default, when you start a new module, either in a form or report, or a global module, Access does not declare Option...
9
3297
by: MrDeej | last post by:
Hello guys! We have an SQL server which sometimes makes timeouts and connection errors. And we have an function witch writes and updates data in 2 tables on this server. When the SQL server error appears it, in 99%, of the cases, works if we just press the play button in VBA debug. Therefor we have maked an error handling which just tryes again. However, as this error handling is difficult to test because of maybe 1 or 2 errors a day, we...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10113
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.