473,804 Members | 3,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pre / post conditions in error handling


Hello all,

I found Herb Sutter's article regarding error handling in the most recent
CUJ to be very, very good. However, in trying to apply what I learned from
the article, I am left with some questions.

I have a function that takes a std::string (that represents a file name) as
a parameter. This function reads in the contents of the file and returns a
struct representing the file's content.

Two things could go wrong inside the function:

1. The function might find that the file does not exist
2. The function might find that the content of the file is invalid

(Some might consider 1 to be a special case of 2.)

The question is: Are these cases a violation of the function's preconditions
or postconditions? According to the error handling philosophy advocated by
Herb in his article, if it is a precondition violation, it should have been
reported as an error in the calling function. Since it wasn't, we should
report a program bug via an assert in the called function. If it is a
postcondition violation, it should be reported as an error in the called
function. Since whether the problem is a precondition or postcondition
problem determines where the error gets reported, it is something I need to
know to properly write my code.

I can reason that these are precondition violations since it seems
justifiable to say that a precondition for the function is that the file
both exist and have valid content.

However, I can also make a reasonable argument that it is a postcondition
violation since the function cannot construct a valid return value. In this
case, the precondition would be nothing more than that the string object
passed in be valid. If it's a valid std::string, the precondition is met,
period.

So, I need a little more guidance than that provided in the article as to
how to classify a problem as a precondition or postcondition violation. I'm
thinking that perhaps it is a subjective thing and that either way you go
could be OK as long as you're consistent. All help will be appreciated!

Thanks,
Dave
Jul 22 '05 #1
3 2375
* Dave:

I found Herb Sutter's article regarding error handling in the most recent
CUJ to be very, very good.
Haven't read that. Link?

However, in trying to apply what I learned from
the article, I am left with some questions.

I have a function that takes a std::string (that represents a file name) as
a parameter. This function reads in the contents of the file and returns a
struct representing the file's content.

Two things could go wrong inside the function:

1. The function might find that the file does not exist
2. The function might find that the content of the file is invalid

(Some might consider 1 to be a special case of 2.)

The question is: Are these cases a violation of the function's preconditions
or postconditions? According to the error handling philosophy advocated by
Herb in his article, if it is a precondition violation, it should have been
reported as an error in the calling function. Since it wasn't, we should
report a program bug via an assert in the called function. If it is a
postcondition violation, it should be reported as an error in the called
function. Since whether the problem is a precondition or postcondition
problem determines where the error gets reported, it is something I need to
know to properly write my code.

I can reason that these are precondition violations since it seems
justifiable to say that a precondition for the function is that the file
both exist and have valid content.

However, I can also make a reasonable argument that it is a postcondition
violation since the function cannot construct a valid return value.

In this
case, the precondition would be nothing more than that the string object
passed in be valid. If it's a valid std::string, the precondition is met,
period.

So, I need a little more guidance than that provided in the article as to
how to classify a problem as a precondition or postcondition violation. I'm
thinking that perhaps it is a subjective thing and that either way you go
could be OK as long as you're consistent. All help will be appreciated!


It's very easy: don't have condition X as a precondition unless the
caller _can_ ensure that condition X holds (and in that case, if you
choose to have X as precondition then a violation is a logic error).

When you don't have condition X as precondition, but X is needed to
return a useful result: if X can be checked for, then it is a design
issue -- what's most practical -- whether the function should
guarantee the useful result on successful execution, or whether it
should also have X-didn't-hold as a possible normal case result.

A guaranteed useful result means X violation should throw exception.

Otherwise, one way of reporting a possibly "null" result is by using a
wrapper object that either is empty or logically contains the useful
result. The case where the useful result is a simple or small value
type has its own little pattern name, which I forget...

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

"Alf P. Steinbach" <al***@start.no > wrote in message
news:40******** ********@news.i ndividual.net.. .
* Dave:

I found Herb Sutter's article regarding error handling in the most recent CUJ to be very, very good.


Haven't read that. Link?

However, in trying to apply what I learned from
the article, I am left with some questions.

I have a function that takes a std::string (that represents a file name) as a parameter. This function reads in the contents of the file and returns a struct representing the file's content.

Two things could go wrong inside the function:

1. The function might find that the file does not exist
2. The function might find that the content of the file is invalid

(Some might consider 1 to be a special case of 2.)

The question is: Are these cases a violation of the function's preconditions or postconditions? According to the error handling philosophy advocated by Herb in his article, if it is a precondition violation, it should have been reported as an error in the calling function. Since it wasn't, we should report a program bug via an assert in the called function. If it is a
postcondition violation, it should be reported as an error in the called
function. Since whether the problem is a precondition or postcondition
problem determines where the error gets reported, it is something I need to know to properly write my code.

I can reason that these are precondition violations since it seems
justifiable to say that a precondition for the function is that the file
both exist and have valid content.

However, I can also make a reasonable argument that it is a postcondition violation since the function cannot construct a valid return value.

In this
case, the precondition would be nothing more than that the string object
passed in be valid. If it's a valid std::string, the precondition is met, period.

So, I need a little more guidance than that provided in the article as to how to classify a problem as a precondition or postcondition violation. I'm thinking that perhaps it is a subjective thing and that either way you go could be OK as long as you're consistent. All help will be appreciated!


It's very easy: don't have condition X as a precondition unless the
caller _can_ ensure that condition X holds (and in that case, if you
choose to have X as precondition then a violation is a logic error).

When you don't have condition X as precondition, but X is needed to
return a useful result: if X can be checked for, then it is a design
issue -- what's most practical -- whether the function should
guarantee the useful result on successful execution, or whether it
should also have X-didn't-hold as a possible normal case result.

A guaranteed useful result means X violation should throw exception.

Otherwise, one way of reporting a possibly "null" result is by using a
wrapper object that either is empty or logically contains the useful
result. The case where the useful result is a simple or small value
type has its own little pattern name, which I forget...

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


As far as a link goes, there isn't one available at this time as the article
is in print. If anyone else knows of a link I didn't find, please post...
Thanks!
Jul 22 '05 #3
"Dave" <be***********@ yahoo.com> wrote in message
Two things could go wrong inside the function:

1. The function might find that the file does not exist
2. The function might find that the content of the file is invalid

(Some might consider 1 to be a special case of 2.)

The question is: Are these cases a violation of the function's preconditions or postconditions? According to the error handling philosophy advocated by Herb in his article, if it is a precondition violation, it should have been reported as an error in the calling function. Since it wasn't, we should
report a program bug via an assert in the called function. If it is a
postcondition violation, it should be reported as an error in the called
function. Since whether the problem is a precondition or postcondition
problem determines where the error gets reported, it is something I need to know to properly write my code.


Precondition and postconditions should test constraints between the member
variables of the class, as far as I know. For the two things that could go
wrong, I don't see a straightforward way to fit them into this scheme. So
my design decision is to throw an exception.
These seem to be
Jul 22 '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.
3
2352
by: Eitan | last post by:
Hello, I want to emphasize a point for my prior posts : How can I trap a message in ASP (not dotnet), to a specific label. (I know : on error goto my_label ... but this does not work in ASP pages) Thanks :)
6
8470
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
4489
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...
2
4315
by: Luis Esteban Valencia Muñoz | last post by:
I have a 2 base classes that do error handling -- one for pages (System.Web.UI.Page) and one for applications (System.Web.HttpApplication, Global.asax uses it). Are there any situations in either of these error handlers where HttpContext.Current would be null? Page Error Handler public class PageBase : System.Web.UI.Page {
10
2298
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
8
2459
by: sara | last post by:
I have a report that runs fine with data. If there is no data, I have its NO Data event sending a MsgBox and cancelling the report. Then it seems I still get the 2501 message on the Open Report command, even though I have the code to trap Err 2501 (from many postings - all looked the same to me) on the button the user pressed to get the report. I never see the code going to my error handling on the button. If I debug, I am in an...
2
1369
by: P Pulkkinen | last post by:
Hi all! I am inspired to develop a regular kind of post to this group and php.general , that would answer let's say 40-60 most typical error/code questions and also give help how to avoid errors in advance and how to debug in an effective way. This faq-post would always be "in process" and anyone could help to maintain it. But someone (doesn't need to be me) would be the Official Maintainer of faq-post. But this depends also in group...
9
3300
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
9576
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
10567
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
10323
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
10310
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,...
0
10074
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
5515
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...
1
4291
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
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2983
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.