Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #1653
| From | Adam Skutt <askutt@gmail.com> |
|---|---|
| Newsgroups | comp.programming, comp.lang.c++ |
| Subject | Re: Error codes vs. exceptions |
| Date | 2012-05-29 07:16 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <7c91ba93-455e-435a-b67a-b9b2d55761be@f30g2000vbz.googlegroups.com> (permalink) |
| References | <4f571f71-55ca-4922-b81b-31f954cac855@kw17g2000pbb.googlegroups.com> <jq1ls9$48j$1@news.albasani.net> |
Cross-posted to 2 groups.
On May 29, 1:10 am, BGB <cr88...@hotmail.com> wrote: > On 5/28/2012 11:15 PM, mike3 wrote: > > > Hi. > > > I've heard about this, and wonder when is it right to use codes, and > > when to use exceptions for reporting errors? I've heard various stuff, > > such as that exceptions should only be used to indicate "exceptional" > > conditions. Yet what does that mean? I've heard that, e.g. a user > > inputting invalid input should not be considered "exceptional", but > > something like running out of memory should be. Does this mean that if > > we have a program, and we have a "parse()" function that parses a > > string input by the user, that this function should return an error > > code on parse failure, instead of throwing an exception? Yet we'll > > probably also come across places where it's good to use an exception, > > in the same program! Which means we get into _mixing error codes and > > exceptions_. And what's the best way to do that? > > maybe more like this: > use error codes if it can be reasonably done so; > use exceptions if error codes wont really work. Nonsense. A function should only return a value if the caller can use the return value to further its own computation. It is very rare for an error code to be useful to the caller, much less all of the codes returned by a function. Hence, they should be exceptions because stack unwinding will be needed. > usually, this means returning an error code if the operation can be > handled as a no-op and otherwise the program can continue as normal > (like, "well, that didn't work"). If the error can and should be treated as a no-op then there is no reason to return anything at all. > if it is not clear to use, probably I would opt with error codes. > (usually, when an exception is needed, it is "fairly obvious"). This is precisely backwards. Return values should have clear, obvious, and unambiguous usage. If you cannot determine what the caller would do with the return value, then odds are good they will "pass the buck" and therefore an exception is the right thing. > > Or, and this is what I've been thinking of, use exceptions for every > > error that the user does not have control over, like invalid input > > strings. Would that be OK or excessive use of exceptions? And if we > > are to mix error codes and exceptions, does this mean we should have > > the lists of codes and exceptions correspond + a translator to > > translate between the two? > > if the function returns a void, use it to return an error code; This is nonsense. I assume you meant, "Change the return type to be an error code" but I honestly have no clue. > if the function returns a pointer, one can potentially return NULL, and > have another operation to fetch the actual error code (stored off in a > variable somewhere); This requires either extra parameters or a lot of ugly work to create thread-safe behavior. Which is why error codes are a bad idea. Using them simply pollutes calling code for no clear benefit, especially when stack unwinding is necessary (which is the most common case). Adam
Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar
Error codes vs. exceptions mike3 <mike4ty4@yahoo.com> - 2012-05-28 21:15 -0700
Re: Error codes vs. exceptions BGB <cr88192@hotmail.com> - 2012-05-29 00:10 -0500
Re: Error codes vs. exceptions mike3 <mike4ty4@yahoo.com> - 2012-05-28 22:41 -0700
Re: Error codes vs. exceptions BGB <cr88192@hotmail.com> - 2012-05-29 01:34 -0500
Re: Error codes vs. exceptions Adam Skutt <askutt@gmail.com> - 2012-05-29 07:16 -0700
Re: Error codes vs. exceptions BGB <cr88192@hotmail.com> - 2012-05-29 09:45 -0500
Re: Error codes vs. exceptions Marcel Müller <news.5.maazl@spamgourmet.com> - 2012-05-29 09:08 +0200
Re: Error codes vs. exceptions mike3 <mike4ty4@yahoo.com> - 2012-05-31 13:52 -0700
Re: Error codes vs. exceptions Ian Collins <ian-news@hotmail.com> - 2012-06-01 09:08 +1200
Re: Error codes vs. exceptions mike3 <mike4ty4@yahoo.com> - 2012-05-31 22:32 -0700
Re: Error codes vs. exceptions "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2012-05-29 09:37 +0200
Re: Error codes vs. exceptions "io_x" <a@b.c.invalid> - 2012-05-29 10:00 +0200
Re: Error codes vs. exceptions Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-05-29 12:24 +0000
Re: Error codes vs. exceptions Nobody <nobody@nowhere.com> - 2012-05-30 15:23 +0100
Re: Error codes vs. exceptions BGB <cr88192@hotmail.com> - 2012-05-30 09:40 -0500
Re: Error codes vs. exceptions Adam Skutt <askutt@gmail.com> - 2012-05-30 08:04 -0700
Re: Error codes vs. exceptions BGB <cr88192@hotmail.com> - 2012-05-30 12:01 -0500
Re: Error codes vs. exceptions Jeff Flinn <TriumphSprint2000@hotmail.com> - 2012-05-30 14:18 -0400
Re: Error codes vs. exceptions Patricia Shanahan <pats@acm.org> - 2012-05-30 13:00 -0700
Re: Error codes vs. exceptions Adam Skutt <askutt@gmail.com> - 2012-05-30 21:25 -0700
Re: Error codes vs. exceptions Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-05-30 22:44 -0400
Re: Error codes vs. exceptions Nobody <nobody@nowhere.com> - 2012-05-31 05:04 +0100
Re: Error codes vs. exceptions Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-06-01 21:44 -0400
Re: Error codes vs. exceptions "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-06-02 08:39 +0200
Re: Error codes vs. exceptions BGB <cr88192@hotmail.com> - 2012-06-02 16:31 -0500
Re: Error codes vs. exceptions mike3 <mike4ty4@yahoo.com> - 2012-06-02 15:49 -0700
Re: Error codes vs. exceptions Ian Collins <ian-news@hotmail.com> - 2012-06-03 11:17 +1200
Re: Error codes vs. exceptions Rui Maciel <rui.maciel@gmail.com> - 2012-06-03 16:38 +0100
Re: Error codes vs. exceptions Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-06-03 20:26 -0400
Re: Error codes vs. exceptions gremnebulin <peterdjones@yahoo.com> - 2012-06-20 12:05 -0700
csiph-web