• C

how to hande errors?

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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...
Here's the standard Micro$oft approach

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

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!!
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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."
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....
"A terrible error occurred . Your system is destroyed . We are terribly sorry . Buy a new system . Old windows license is not valid anymore"

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
amer_fAuthor Commented:
for captainkrik my e-mail : amer_f@usa.net
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.