Discussion:
Throwing exceptions
(too old to reply)
Patryk B.
2008-08-06 08:00:39 UTC
Permalink
Hello,

I have question about situation when i should throw exception.

For example i have some function that work with some text file,
function read some data and analyze it. Let's say that in that file should
only by digits but the functions encouter letter (somone who wrote that
file made a mistake and pressed wrong key). So in that situation I should
throw a exception or maybe just "return false"(or other value)? I know that
I can do both, but which method is better, which one is use frequently,
which one is compatible which modern programming style.

Thanks,
Patryk B.
Larry Griffiths
2008-08-06 13:41:12 UTC
Permalink
Personally I like return codes most of the time but if you are creating
objects that could have lots of possible return outcomes then
I like throwing exceptions to let the caller show the message in a catch
statement.

I actually like a combination of both where a call can return a sucess/fail
and throw an exception for conditions outside of the
sucess/fail return code to the caller.

Try/catch code needs to be designed with a hiearchy
in mind when calls go several levels deep.

Larry Griffiths.
Post by Patryk B.
Hello,
I have question about situation when i should throw exception.
For example i have some function that work with some text file,
function read some data and analyze it. Let's say that in that file should
only by digits but the functions encouter letter (somone who wrote that
file made a mistake and pressed wrong key). So in that situation I should
throw a exception or maybe just "return false"(or other value)? I know that
I can do both, but which method is better, which one is use frequently,
which one is compatible which modern programming style.
Thanks,
Patryk B.
Chris Uzdavinis (TeamB)
2008-08-06 14:13:59 UTC
Permalink
Post by Patryk B.
Hello,
I have question about situation when i should throw exception.
For example i have some function that work with some text file,
function read some data and analyze it. Let's say that in that file should
only by digits but the functions encouter letter (somone who wrote that
file made a mistake and pressed wrong key). So in that situation I should
throw a exception or maybe just "return false"(or other value)? I know that
I can do both, but which method is better, which one is use frequently,
which one is compatible which modern programming style.
Both ways are valid, and it depends on your feeling of the situation.
Is it unlikely that the file will ever be corrupted? If so, an
exception may be the best way to represent the problem. Do you expect
that the caller will always have direct interest in the success or
failure of the function call? If so you may prefer an error code
instead. If you plan for this to be in some kind of event handling
loop, then again you might also think that unwinding the stack back to
the main loop is appropriate (exception.)

But all that aside, the real question you need to ask is, "how
important is it for the caller(s) to handle this situation?" If there
can reasonably be cases where the failure is no big deal, then an
exception can be a burdon rather than a help. However, if it's
severe, or it's very important that the problem not be overlooked
(perhaps by accident) then an exception is the best way to hope that
the caller takes notice of the problem.
--
Chris (TeamB);
Larry Griffiths
2008-08-06 16:50:23 UTC
Permalink
Chris wrote:

" However, if it's
severe, or it's very important that the problem not be overlooked
(perhaps by accident) then an exception is the best way to hope that
the caller takes notice of the problem. "

I know all about those callers, Chris. hehe

try
{

}
catch( Exception &E )
{
// Ignore Exception, I see the world through rose colored glasses,
everything is ok, don't try this at home...
}

Loading...