Online Book Reader

Home Category

Professional C__ - Marc Gregoire [159]

By Root 1314 0
{

// We failed to open the file: throw an exception.

throw exception();

}

// Read the integers one by one and add them to the vector.

while (istr >> temp) {

dest.push_back(temp);

}

}

Code snippet from ReadIntegerFile\BasicExceptions.cpp

If the function fails to open the file and executes the throw exception(); line, the rest of the function is skipped, and control transitions to the nearest exception handler.

Throwing exceptions in your code is most useful when you also write code that handles them. Exception handling is a way to “try” a block of code, with another block of code designated to react to any problems that might occur. In the following main() function the catch statement reacts to any exception of type exception that was thrown within the try block by printing an error message. If the try block finishes without throwing an exception, the catch block is skipped. You can think of try/catch blocks as glorified if statements. If an exception is thrown in the try block, execute the catch block. Otherwise, skip it.

int main()

{

vector myInts;

const string fileName = "IntegerFile.txt";

try {

readIntegerFile(fileName, myInts);

} catch (const exception& e) {

cerr << "Unable to open file " << fileName << endl;

return 1;

}

for (size_t i = 0; i < myInts.size(); i++) {

cout << myInts[i] << " ";

}

cout << endl;

return 0;

}

Code snippet from ReadIntegerFile\BasicExceptions.cpp

Although by default, streams do not throw exceptions, you can tell the streams to throw exceptions for error conditions by calling their exceptions( ) method. However, Bjarne Stroustrup, creator of C++, likes to deal with the stream state directly instead of using exceptions (The C++ Programming Language, third edition). This book follows his style in that regard.

Exception Types

You can throw an exception of any type. The preceding example throws an object of type exception, but exceptions do not need to be objects. You could throw a simple int like this:

void readIntegerFile(const string& fileName, vector& dest)

{

// Code omitted

istr.open(fileName.c_str());

if (istr.fail()) {

// We failed to open the file: throw an exception.

throw 5;

}

// Code omitted

}

Code snippet from ReadIntegerFile\ThrowInt.cpp

You would then need to change the catch statement as well:

try {

readIntegerFile(fileName, myInts);

} catch (int e) {

cerr << "Unable to open file " << fileName << endl;

return 1;

}

Code snippet from ReadIntegerFile\ThrowInt.cpp

Alternatively, you could throw a char* C-style string. This technique is sometimes useful because the string can contain information about the exception.

void readIntegerFile(const string& fileName, vector& dest)

{

// Code omitted

istr.open(fileName.c_str());

if (istr.fail()) {

// We failed to open the file: throw an exception.

throw "Unable to open file";

}

// Code omitted

}

Code snippet from ReadIntegerFile\ThrowCharStar.cpp

When you catch the char* exception, you can print the result:

try {

readIntegerFile(fileName, myInts);

} catch (const char* e) {

cerr << e << endl;

return 1;

}

Code snippet from ReadIntegerFile\ThrowCharStar.cpp

In modern programming, you should avoid using 8-bit char* C-style strings. You should use Unicode strings which are discussed in detail in Chapter 14.

Despite the previous examples, you should generally throw objects as exceptions for two reasons:

Objects convey information by their class name.

Objects can store information, including strings that describe the exceptions.

The C++ standard library defines a number of predefined exception classes and you can write your own exception classes. Details are described later in this chapter.

Catching Exception Objects by const and Reference

In the preceding example in which readIntegerFile() throws an object of type exception, the catch line looks like this:

} catch (const exception& e) {

However, there is no requirement to catch objects by const reference. You could catch the object by value like this:

} catch (exception e) {

Code snippet

Return Main Page Previous Page Next Page

®Online Book Reader