Error Managment

J

JackC

Hi,

If I have a long boolean function that has regular error checking
throughout, and simply returns false upon an error, whats the best
approach to getting detailed information about the error in the code
segment that called the function?

For example in a function:
{
....
open filea
check(filea open failed)
return false
open fileb
check(fileb open failed)
return false
....
}

How could i alter this so that the code that calls the above function
can tell if filea or fileb failed with a detailed error message for
the user?

My theoretical approach, which might be wrong is as follows:

I would create a public variable in my class : string LAST_ERROR;

then set this with detailed information right before 'return false',
then the calling segment could read the contents of last error, and
echo it to the user.

Is this the best approach to error handling? I have heard about using
error codes, but i cant think how i this could be implemented, or if
there is a more standardized method.

Thanks for any advice,

Jack
 
?

=?iso-8859-1?q?Elias_Salom=E3o_Helou_Neto?=

Hi,

If I have a long boolean function that has regular error checking
throughout, and simply returns false upon an error, whats the best
approach to getting detailed information about the error in the code
segment that called the function?

For example in a function:
{
...
open filea
check(filea open failed)
return false
open fileb
check(fileb open failed)
return false
...

}

How could i alter this so that the code that calls the above function
can tell if filea or fileb failed with a detailed error message for
the user?

My theoretical approach, which might be wrong is as follows:

I would create a public variable in my class : string LAST_ERROR;

then set this with detailed information right before 'return false',
then the calling segment could read the contents of last error, and
echo it to the user.

Is this the best approach to error handling? I have heard about using
error codes, but i cant think how i this could be implemented, or if
there is a more standardized method.

Thanks for any advice,

Jack


Throw an exception. In the following example. If you have a file named
test in the directory you run the executable obtained by compiling the
program below you get:

Path 2 invalid!

The code:

#include <fstream>
#include <iostream>

class ExceptionA {};
class ExceptionB {};


bool myFun( const char* path1, const char* path2 )
{
std::ifstream fileA( path1 );
if ( !fileA.good() ) throw( ExceptionA() );

std::ifstream fileB(path2);
if ( !fileB.good() ) throw( ExceptionB() );
}

int main( int argc, char* argv[], char* env[] )
{
try
{
myFun( "test", "wrong path" );
}
catch ( const ExceptionA& e )
{
std::cerr << "Path 1 invalid!\n";
}
catch ( const ExceptionB& e )
{
std::cerr << "Path 2 invalid!\n";
}

return( 0 );
}

Notice that you do not actually have to create a class for every
possible exception, but, instead, you may include lots of information
in the thrown object with an appropriate constructor. You can then use
it via the "e" reference caught by the catch clause. There is much
more to that, a good source (not only in this subject) is:

http://www.parashift.com/c++-faq-lite/exceptions.html

Elias Salomão Helou Neto.
 
?

=?iso-8859-1?q?Elias_Salom=E3o_Helou_Neto?=

Sorry, but the above code was somewhat sloppy in that it did not
return any value from the myFun function if it succeeds. Even though
the program compiles and runs, this would better be corrected.

Notice that when throwing, however, the return statement would make no
sense anyway.
 
J

James Kanze

If I have a long boolean function

You have a problem. Functions shouldn't be long.
that has regular error checking
throughout, and simply returns false upon an error,

Not a very good choice, I would think. A boolean says true or
false; it answers a yes or no question. ("isEmpty()", for
example.) It doesn't say succeeded or failed, much less why
something failed.
whats the best approach to getting detailed information about
the error in the code segment that called the function?

Returning the information in the return value.
For example in a function:
{
...
open filea
check(filea open failed)
return false
open fileb
check(fileb open failed)
return false
...
}
How could i alter this so that the code that calls the above function
can tell if filea or fileb failed with a detailed error message for
the user?

I doubt that you want a detailed error message at this point;
just the necessary information so that higher level code can
formulate the message. (At this level, you probably don't want
to be concerned with issues of internationalization, for
example.)
My theoretical approach, which might be wrong is as follows:
I would create a public variable in my class : string LAST_ERROR;
then set this with detailed information right before 'return false',
then the calling segment could read the contents of last error, and
echo it to the user.

Sounds horrible. Just because Unix and Windows have adopted
more or less this approach (errno, GetLastError) doesn't mean
that it's a good idea.
Is this the best approach to error handling? I have heard
about using error codes, but i cant think how i this could be
implemented, or if there is a more standardized method.

The usual situation, when there is a need only to report an
error, and no additional need for information in the absense of
an error, is tu use a return code. Just define an enum with OK
andthe possible errors, and return it. Or wrap it in a class,
which will abort if the destructor is called without the error
having been checked.
 
J

Jim Langston

JackC said:
Hi,

If I have a long boolean function that has regular error checking
throughout, and simply returns false upon an error, whats the best
approach to getting detailed information about the error in the code
segment that called the function?

For example in a function:
{
...
open filea
check(filea open failed)
return false
open fileb
check(fileb open failed)
return false
...
}

How could i alter this so that the code that calls the above function
can tell if filea or fileb failed with a detailed error message for
the user?

My theoretical approach, which might be wrong is as follows:

I would create a public variable in my class : string LAST_ERROR;

then set this with detailed information right before 'return false',
then the calling segment could read the contents of last error, and
echo it to the user.

Is this the best approach to error handling? I have heard about using
error codes, but i cant think how i this could be implemented, or if
there is a more standardized method.

As Elas showed, you should throw an exception, although you should inherit
from std::exception. See FAQ 17.6
http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.6

The output of the following program is:
Path1 bad

#include <stdexcept>
#include <fstream>
#include <iostream>

class MyException : public std::runtime_error
{
public:
MyException(const std::string& What ): std::runtime_error(What) { }
};

bool myFun( const char* path1, const char* path2 )
{
std::ifstream fileA( path1 );
if ( !fileA.good() )
throw( MyException("Path1 bad") );

std::ifstream fileB(path2);
if ( !fileB.good() )
throw( MyException("Path2 bad") );

return true;
}

int main()
{
try
{
myFun( "Foo.txt", "Bar.txt" );
}
catch ( std::exception& e )
{
std::cout << e.what();
}
}

I took the exception and modified it from the FAQ. I'm not sure, but you may
be ablet o simply throw std::exception( What ); I don't know, didnt' test
it. Excercise for the reader.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,289
Messages
2,571,449
Members
48,127
Latest member
svastipharmancrr

Latest Threads

Top