Reputation: 6655
I have a c program which defines the following function:
void logFatal(char *msg, enum errors error)
{
fprintf(stderr, msg);
exit(error);
}
In the rest of the program if anything happens that means the program will not function properly if execution continues (typical example being a call to malloc
fails to allocate memory), then I call logFatal
and the program exits, where-ever it happens to be.
Is this good/bad practice?
Upvotes: 0
Views: 581
Reputation: 2220
As long as you are not writing a library for other people ;) you are free to do this. It is usually used as a last resource though, because with most errors it is enough to just cancel the action.
Imagine this error:
That feels a bit harsh... So in that case it is better to handle the error with a message to the user etc. But since you didn't specify what kind of application, i can't tell you anything about that, but malloc failures usually indicate big trouble, so in that case it's probably best to just exit.
Upvotes: 1