473,606 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check all errors in code?

Do you check all error conditions for all library calls in you code?

Is it necessary to check all errors? Is it convenient to check all
errors (or how to make code clean and readable with mass of non
business logic related code block)?

The following code from net-snmp library calls atoi five times but
checks none. Do we code a wrapper my_atoi_with_er ror_check() around
atoi and call this new one everywhere else? Is it a good practice (If
it is, why ANSI atoi doesn't do it)?

/* <quote file=../net-snmp-5.4.1.1/snmplib/vacm.c*/

286 char *
287 _vacm_parse_con fig_access_comm on(struct vacm_accessEntr y
**aptr, char *line)
288 {
289 struct vacm_accessEntr y access;
290 char *cPrefix = (char *) &access.context Prefix;
291 char *gName = (char *) &access.groupNa me;
292 size_t len;
293
294 access.status = atoi(line);
295 line = skip_token(line );
296 access.storageT ype = atoi(line);
297 line = skip_token(line );
298 access.security Model = atoi(line);
299 line = skip_token(line );
300 access.security Level = atoi(line);
301 line = skip_token(line );
302 access.contextM atch = atoi(line);
303 line = skip_token(line );
304 len = sizeof(access.g roupName);
305 line = read_config_rea d_octet_string( line, (u_char **)
&gName, &len);
306 len = sizeof(access.c ontextPrefix);
307 line = read_config_rea d_octet_string( line, (u_char **)
&cPrefix, &len);
308
309 *aptr = vacm_getAccessE ntry(access.gro upName,
310 access.contextP refix,
311 access.security Model,
312 access.security Level);
313 if (!*aptr)
314 *aptr = vacm_createAcce ssEntry(access. groupName,
315 access.contextP refix,
316 access.security Model,
317 access.security Level);
318 if (!*aptr)
319 return NULL;
320
321 (*aptr)->status = access.status;
322 (*aptr)->storageType = access.storageT ype;
323 (*aptr)->securityMode l = access.security Model;
324 (*aptr)->securityLeve l = access.security Level;
325 (*aptr)->contextMatch = access.contextM atch;
326 return line;
327 }

/* </quote*/
Sep 19 '08 #1
55 3283
lovecreatesbea. ..@gmail.com wrote:
Do you check all error conditions for all library calls in you code?

Is it necessary to check all errors? Is it convenient to check all
errors (or how to make code clean and readable with mass of non
business logic related code block)?

The following code from net-snmp library calls atoi five times but
checks none. Do we code a wrapper my_atoi_with_er ror_check() around
atoi and call this new one everywhere else? Is it a good practice (If
it is, why ANSI atoi doesn't do it)?
There is no way to check the result of atoi, that's why it's better to
use strtol in most situations.

--
Ian Collins.
Sep 19 '08 #2
On Sep 19, 4:08 pm, Ian Collins <ian-n...@hotmail.co mwrote:
lovecreatesbea. ..@gmail.com wrote:
Do you check all error conditions for all library calls in you code?
Is it necessary to check all errors? Is it convenient to check all
errors (or how to make code clean and readable with mass of non
business logic related code block)?
The following code from net-snmp library calls atoi five times but
checks none. Do we code a wrapper my_atoi_with_er ror_check() around
atoi and call this new one everywhere else? Is it a good practice (If
it is, why ANSI atoi doesn't do it)?

There is no way to check the result of atoi, that's why it's better to
use strtol in most situations.
I did not just mean check the return code from a function call.

Especially, I'm asking how to do complicated error check neatly.
Sep 19 '08 #3
lovecreatesbea. ..@gmail.com said:
Do you check all error conditions for all library calls in you code?
It has been said that you should never check for a condition you don't know
how to handle. But apart from that, check everything.
Is it necessary to check all errors?
Is it useful to know whether your program worked? If not, then the program
itself is probably not all that useful.
The following code from net-snmp library calls atoi five times but
checks none.
Feel free to explain how you'd check atoi. Once you've failed to do that,
you will be in a position to explain why the code is broken.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 19 '08 #4
lovecreatesbea. ..@gmail.com wrote:
On Sep 19, 4:08 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>lovecreatesbea ...@gmail.com wrote:
>>Do you check all error conditions for all library calls in you code?
Is it necessary to check all errors? Is it convenient to check all
errors (or how to make code clean and readable with mass of non
business logic related code block)?
The following code from net-snmp library calls atoi five times but
checks none. Do we code a wrapper my_atoi_with_er ror_check() around
atoi and call this new one everywhere else? Is it a good practice (If
it is, why ANSI atoi doesn't do it)?
There is no way to check the result of atoi, that's why it's better to
use strtol in most situations.

I did not just mean check the return code from a function call.

Especially, I'm asking how to do complicated error check neatly.
Use a language with exceptions :)

--
Ian Collins.
Sep 19 '08 #5
On Sep 19, 4:08*pm, Ian Collins <ian-n...@hotmail.co mwrote:
lovecreatesbea. ..@gmail.com wrote:
Do you check all error conditions for all library calls in you code?
Is it necessary to check all errors? Is it convenient to check all
errors (or how to make code clean and readable with mass of non
business logic related code block)?
The following code from net-snmp library calls atoi five times but
checks none. Do we code a wrapper my_atoi_with_er ror_check() around
atoi and call this new one everywhere else? Is it a good practice (If
it is, why ANSI atoi doesn't do it)?

There is no way to check the result of atoi, that's why it's better to
use strtol in most situations.
I ever thought there're errno for atoi also. Thanks for reminder.
Sep 19 '08 #6
On 19 Sep, 09:16, "lovecreatesbea ...@gmail.com"
<lovecreatesbea ...@gmail.comwr ote:
On Sep 19, 4:08 pm, Ian Collins <ian-n...@hotmail.co mwrote:
lovecreatesbea. ..@gmail.com wrote:
Do you check all error conditions for all library calls in you code?
Is it necessary to check all errors? Is it convenient to check all
errors (or how to make code clean and readable with mass of non
business logic related code block)?
The following code from net-snmp library calls atoi five times but
checks none. Do we code a wrapper my_atoi_with_er ror_check() around
atoi and call this new one everywhere else? Is it a good practice (If
it is, why ANSI atoi doesn't do it)?
There is no way to check the result of atoi, that's why it's better to
use strtol in most situations.

I did not just mean check the return code from a function call.

Especially, I'm asking how to do complicated error check neatly.
as someone else suggested, use exceptions.

Out of interest I've coded simple TCP/IP applications
in both C and Python. The Python versions were shorter
and easier to follow.

I somtimes use an exception like style in C

Rv read_box (Db_handle dbh, Box* box, Box_id box_id)
{
Rv rv;
int i;

for (i = 1; i < SHELF_COUNT; i++)
{
if ((rv = read_shelf (dbh, box->shelf[i], box_id, i)) != OK)
return rv;
}

return OK;
}

This was wrappered in macros so the code looked more like:

Rv read_box (Db_handle dbh, Box* box, Box_id box_id)
{
int i;

for (i = 1; i < SHELF_COUNT; i++)
{
read_shelf (dbh, box->shelf[i], box_id, i);
CHECK_ERROR;
}

return OK;
}

It was reasonably easy to follow and reasonably
easy to verify that all the error checking was done.
--
Nick Keighley

"Object-oriented programming is an exceptionally bad idea
that could only have originated in California."
Dijkstra
Sep 19 '08 #7
On Sep 19, 4:28 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
lovecreatesbea. ..@gmail.com said:
Do you check all error conditions for all library calls in you code?

It has been said that you should never check for a condition you don't know
how to handle. But apart from that, check everything.
Sometimes, I omit some error check to come up with a can-work module
rapidly. Those error code will be supplied after some bug report
thrown out. This approach helps to catch up the schedule.
Is it necessary to check all errors?

Is it useful to know whether your program worked? If not, then the program
itself is probably not all that useful.
Thanks for being patient.
The following code from net-snmp library calls atoi five times but
checks none.

Feel free to explain how you'd check atoi. Once you've failed to do that,
you will be in a position to explain why the code is broken.
That snippet is radomly selected. They even do not check the malloc
for three times, see Line 55, 59 and 64 below.

Dude, was it you who wrote atoi piece plus this one?
/* <quote file=../net-snmp-5.4.1.1/agent/auto_nlist.c*/
34 long
35 auto_nlist_valu e(const char *string)
36 {
37 struct autonlist **ptr, *it = 0;
38 int cmp;
39
40 if (string == 0)
41 return 0;
42
43 ptr = &nlists;
44 while (*ptr != 0 && it == 0) {
45 cmp = strcmp((*ptr)->symbol, string);
46 if (cmp == 0)
47 it = *ptr;
48 else if (cmp < 0) {
49 ptr = &((*ptr)->left);
50 } else {
51 ptr = &((*ptr)->right);
52 }
53 }
54 if (*ptr == 0) {
55 *ptr = (struct autonlist *) malloc(sizeof(s truct
autonlist));
56 it = *ptr;
57 it->left = 0;
58 it->right = 0;
59 it->symbol = (char *) malloc(strlen(s tring) + 1);
60 strcpy(it->symbol, string);
61 /*
62 * allocate an extra byte for inclusion of a preceding
'_' later
63 */
64 it->nl[0].n_name = (char *) malloc(strlen(s tring) +
2);
65 #if defined(aix4) || defined(aix5)
66 strcpy(it->nl[0].n_name, string);
67 #else
68 sprintf(it->nl[0].n_name, "_%s", string);
69 #endif
70 it->nl[1].n_name = 0;
71 init_nlist(it->nl);
72 #if !(defined(aix4) || defined(aix5))
73 if (it->nl[0].n_type == 0) {
74 strcpy(it->nl[0].n_name, string);
75 init_nlist(it->nl);
76 }
77 #endif
78 if (it->nl[0].n_type == 0) {
79 if (!
netsnmp_ds_get_ boolean(NETSNMP _DS_APPLICATION _ID,
80
NETSNMP_DS_AGEN T_NO_ROOT_ACCES S)) {
81 snmp_log(LOG_ER R, "nlist err: neither %s nor _
%s found.\n",
82 string, string);
83 }
84 return (-1);
85 } else {
86 DEBUGMSGTL(("au to_nlist:auto_n list_value", "found
symbol %s at %x.\n",
87 it->symbol, it->nl[0].n_value));
88 return (it->nl[0].n_value);
89 }
90 } else
91 return (it->nl[0].n_value);
92 }

/* </quote*/
Sep 19 '08 #8
On 19 Sep, 09:54, "lovecreatesbea ...@gmail.com"
<lovecreatesbea ...@gmail.comwr ote:
On Sep 19, 4:28 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
lovecreatesbea. ..@gmail.com said:
Do you check all error conditions for all library calls in you code?
It has been said that you should never check for a condition you don't know
how to handle. But apart from that, check everything.

Sometimes, I omit some error check to come up with a can-work module
rapidly. Those error code will be supplied after some bug report
thrown out. This approach helps to catch up the schedule.
not in my experience! I'd much rather put the checks in at the
beginning.
And if you had the checks you wouldn't need to debug- your program
would tell you what was wrong.

<snip>

--
Nick Keighley
Sep 19 '08 #9
On Sep 19, 5:19*pm, Nick Keighley <nick_keighley_ nos...@hotmail. com>
wrote:
On 19 Sep, 09:54, "lovecreatesbea ...@gmail.com"

<lovecreatesbea ...@gmail.comwr ote:
On Sep 19, 4:28 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
lovecreatesbea. ..@gmail.com said:
Do you check all error conditions for all library calls in you code?
It has been said that you should never check for a condition you don't know
how to handle. But apart from that, check everything.
Sometimes, I omit some error check to come up with a can-work module
rapidly. Those error code will be supplied after some bug report
thrown out. This approach helps to catch up the schedule.

not in my experience! I'd much rather put the checks in at the
beginning.
And if you had the checks you wouldn't need to debug- your program
would tell you what was wrong.
Yes, your way is right way.
Sep 19 '08 #10

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

Similar topics

2
11851
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an error message and stop the process every time someone loads the script after the table is created because that would mean the page could only ever run once which of course not the solution I was looking for. I simply want to know how I can check...
6
4732
by: joethis | last post by:
Is there a way to make sure that a file is already in use using asp? For instance, if one person has opened a file and is about to write to it; then is there a way to keep another user from reading, or writing to that text file until the first user is finished?
4
7733
by: Stan R. | last post by:
Hello, I have xmllint and xsltproc installed and running on both my linux and win32 platforms, all seems good. I have a couple questions though. 1) If I have an external dtd file, which is included in my xml file, like <!DOCTYPE userlist SYSTEM "test.dtd">
18
2738
by: Joel Hedlund | last post by:
Hi! The question of type checking/enforcing has bothered me for a while, and since this newsgroup has a wealth of competence subscribed to it, I figured this would be a great way of learning from the experts. I feel there's a tradeoff between clear, easily readdable and extensible code on one side, and safe code providing early errors and useful tracebacks on the other. I want both! How do you guys do it? What's the pythonic way? Are...
3
3547
by: eliasen | last post by:
Hi I would like to run through an XML file using C# 2.0 and check for well formedness - and I would like to get all errors and not jsut the first one. My code is, off course, very simple: XmlTextReader xtr = new XmlTextReader(xmlInstanceTextBox.Text); try {
14
1757
by: Martin Wells | last post by:
When I have errors in a program, whether they be compiler errors, linker errors, or if the program crashes when running, I have a list of things I check for initially. If I get an error for something undeclared, the first thing I do is go through my header files and check that the inclusion-guard macro is specific to that header file. For instance, if the header file is called "data.h", I make sure the inclusion guard is "H__DATA" and...
173
8050
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc. OTOH, if you're allocating a gigabyte for a large array, this might fail, so you should definitely check for a NULL return.
3
1711
by: John Wright | last post by:
I have a VB program that will be generating documentation that will be stored for 60 years. This program really needs to have spell check and I read the post below on using spell check. I was wondering if there was a way to tap into the Word spell check that shows the underlined spelling errors as the person types. If so, can someone point me to some code to try? It would be really nice to show the errors as they type (like word does)...
4
1489
by: kilabyte | last post by:
Hi, First post on here, I can't see that the question has been asked before. I am new to Visual Studio 2008 (using VB), but a seasoned programmer using Access and VBA. I have created a web site in VS and a form that the user fills in on a web page. The results are written to a record on a server once the send button is clicked on. 10 items on the screen are validated (is a named entered, is an address entered, do money fields have a...
0
8024
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
8449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8432
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...
0
8310
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6781
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3942
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3987
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2451
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
0
1305
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.