Index:
[thread]
[date]
[subject]
[author]
From: Garrett Mitchener <wgm2@acpub.duke.edu>
To :
Date: 08 Feb 1999 21:26:22 -0500
Re: Error command?
Angelique Coviello <amc1@duke.edu> writes:
> I just tried to post this but I don't think it worked, sorry if it comes
> up twice.
>
> I'm wondering if there is a way to exit the program from within a class
> if the user enters an invalid expression. I'm remembering something to
> the effect of exit(1), but I have no idea where it can be used (only in
> main or can it be from a class called by main?), what exactly it does, or
> the proper syntax and include statement. Thanks for any help.
>
> Angel
I realize this reply is a little late, but there's an interesting
design issue here about error handling.
You may call exit from anywhere, and it will terminate your whole
program. It's just a plain old function that happens to make a call
to the operating system telling it to terminate the current program.
However, you should be careful where you use it for design reasons.
Suppose you have a class that calls exit whenever something doesn't go
right (a file doesn't exist, the arguments are bad, etc.). Then that
class can't be used in any other program where you might want to
handle an error differently. Suppose you write a GUI and someone
tries to open a file that doesn't exist. Suppose you're writing a
spreadsheet and the user enters "a=b+" by accident. You don't want
the program to terminate, and you also don't want for just an error
message to appear on the console (as in cerr << "you goofed").
I suggest you not use exit() or cerr <<, but either:
* return some value from the function that indicates an error
* get really ambitious and try to throw an exception
* define an abstract error handler class. Then people who want to use
your code can write their own error handlers that fit the situation.
(*This solution, as easy as it may seem, involves some more serious
design issues and whether or not to use a Singleton...)
Think about error handling!
-- Garrett *<:-)
Index:
[thread]
[date]
[subject]
[author]