how to hande errors?

Posted on 1999-12-08
Medium Priority
Last Modified: 2010-04-15
generally when i write a C program which requires an interface i make a function which handle this interface (ex. function f1 which reads the file name from the user) then i call from f1 function f2 which is the main program and output of f2 is returned to f1 to be printed , sometimes errors occurs(ex. cannot open the input file) i want to know where to put the error handler and how to deal with user's errors and if handling the errors is a subject to study where can i find something talking about this?
Question by:amer_f

Expert Comment

ID: 2265607
Straightforward error handling in C is mostly checking the return codes of functions and doing a combination of the following actions:

1) printing out a message
2) showing an error dialog
3) logging the error
4) determining whether or not to continue processing

You can define a separate function which processes Win32 error codes and performs the appropriate actions(s). Any return codes from user-defined functions you will have to treat separately.

C++ has try/catch blocks for exception handling, but that's not available in C...
LVL 18

Expert Comment

ID: 2265642
Here's the standard Micro$oft approach

printf("An error has occurred in Module 876876843:987210973 of your system - please reboot");
LVL 32

Expert Comment

ID: 2265868

You forgot my favorite:

"An error has occurred. Please contact your system administrator."

But that's _ME_ and I don't have a clue what's wrong!!
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

LVL 32

Expert Comment

ID: 2265876
Actually, this is how all errors will be handled in the future:

"An error has occurred.  Please call 1-900-HELP-ME for assistance.  Be aware that you will be billed $49.95 for this call and incur a per-minute charge of $9.95 during the call.  Thank you for choosing our wonderful product."

Expert Comment

ID: 2265886
If anybody's interested, I developed an error program that will keep ya amused for awhile... send me yer emails and I'll ship it....

Expert Comment

ID: 2265953
"A terrible error occurred . Your system is destroyed . We are terribly sorry . Buy a new system . Old windows license is not valid anymore"


Accepted Solution

azami earned 40 total points
ID: 2266037
Error handling is a HUGE topic - I've stated that there should be entire courses devoted to it, but I know of none.  Here are a few principles:

1 - catch the error at a level that you can do something about it
2 - don't present a user interface when low-level (non-UI) code handles an error
3 - if you need a UI for the error, propogate it out to code that is already in charge of your UI
4 - NEVER bury an exit(0) in low-level code

There are two big goals - one is to handle errors locally where you can still do something reasonable with them, and the other is to handle errors centrally, so that you don't end up with widely scattered error handling code.

Possibly the best way to do it is to handle the error the best you can at the lowest level, and if you can't fix the problem there, propogate it out to the next level.  In C++, "propogate it out" probably means throwing an exception to the next try/catch block up; in C, it probably means returning an error code.

There are people who will argue whether it's better to return an error code or throw an exception.  One potentially big difference between the two is in the amount and type of information you can get.  For example, some functions for writing to a stream in C return the number of characters written, even when they fail - this is useful information for recovering from an error that occurs after some bytes are written.  Similar functions in C++ often throw exceptions, and offer no way to find out how many bytes actually made it into the stream.

In your specific case, I think it is vitally important that f2() does absolutely no printing, even if there is an error.  Since f1() knows how to get the file name, and a reasonable course of action is for the user to try again with a new name, it seems like f2() should return an error code.  f1() can display the error and prompt the user to try a new file name or quit.

Oh, one other thing - error messages should not be cryptic or vague.  Be sure to describe concisely, in terms the user can understand, what went wrong, what the software is doing/has done about it, and what action(s) the user should take.

Author Comment

ID: 2266173
for captainkrik my e-mail : amer_f@usa.net

Featured Post

Become an IT Security Management Expert

In today’s fast-paced, digitally transformed world of business, the need to protect network data and ensure cloud privacy has never been greater. With a B.S. in Network Operations and Security, you can get the credentials it takes to become an IT security management expert.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question