Download Special Edition Using Visual C++ 6

Transcript
Special Edition Using Visual C++ 6 -- Ch 26 -- Exceptions and Templates
general as you can get, catching every single exception that the program throws. In this case
(as in most cases), you want to use catch(...) to receive only the leftover exceptions.
Listing 26.6 EXCEPTION6.CPP--Using Multiple catch Blocks
#include <iostream.h>
class MyException
{
protected:
char* m_msg;
public:
MyException(char *msg) { m_msg = msg;}
~MyException(){}
char* GetError() {return m_msg;}
};
int GetValue();
int main()
{
try
{
int value = GetValue();
cout << "The value you entered is okay." << endl;
}
catch(MyException* exception)
{
char* msg = exception->GetError();
cout << msg << endl;
}
catch(char* msg)
{
cout << msg << endl;
}
catch(...)
{
cout << "Caught unknown exception!" << endl;
}
return 0;
}
int GetValue(){
int value;
cout << "Type a number from 4 to 8 (except 6):" << endl;
cin >> value;
if (value < 4)
{
MyException* exception =
http://www.pbs.mcp.com/ebooks/0789715392/ch26/ch26.htm (10 of 23) [7/29/1999 3:57:14 PM]