Thinking in C++ 有问题??!!第17chapter,异常处理程序有问题!有这个头文件吗?
各位大侠:
我在学习c++编程思想的时候,第17chapter,异常处理总是有一个<except.h>头文件,通过vc6.0在编译的时候总是过不去,对于:except.cpp该例:发现set_unexpected()函数是<eh.h>中定义的,将<except.h>改为<eh.h>并且将for(int i=1; i<=3; i++)改为for(int i=1; i<=2; i++)因为源程序中只有两个抛出类。编译通过!
而在:17.4清除中,我就不知道该如何调试了:源程序如下:
//:CLEANUP.CPP --- Exceptions clear up objects
#include <fstream.h>
#include <except.h> //c++中有这个头文件吗?我在msdn中也找不到哦!
#include <string.h>
#include <eh.h> //for set_unexpected() ,此处seal加上!
ofstream out ("cleanup.out");
class noisy
{
static int i;
int objnum;
enum { sz = 40 };
char name [sz];
public:
noisy(const char *nm = "array elem ") //throw (int)
{
objnum=i++;
memset(name ,0,sz);
strncpy(name, nm, sz -1);
out << "construcing noisy " << objnum
<< " name [ "<< name <<" ] " << endl;
if ( objnum ==5 ) throw int ( 5 );
//Not in exception specification :
if ( *nm == 'z') throw char ( 'z' );
}
~noisy()
{
out << "destructing noisy " << objnum
<< "name [" << name << "] " << endl;
}
void *operator new [] ( size_t sz )
{
out << " noisy::new []" << endl;
return ::new char [sz];
}
void operator delete [] ( void * p)
{
out << "noisy::delete[]" << endl;
::delete []p;
}
};
int noisy::i =0;
void unexpected_rethrow()
{
out << "inside unexpected_rethrow()" << endl;
throw; //Rethrow same exception
}
void main()
{
set_unexpected (unexpected_rethrow);
try
{
noisy nl( "before array" );
// Throws exception:
noisy * array = new noisy [7];
noisy n2("after array");
}
catch (int i)
{
out << "caught" << i << endl;
}
out << "testing unexpected:" << endl;
try
{
noisy n3 ("before unexpected");
noisy n4 ("z");
noisy n5 ("after unexpected");
}
catch (char c)
{
out << "caught" << c << endl;
}
}
此时,编译不能通过!重新将://include <except.h> ,编译通过,但是没有捕获到任何异常啊??好像不对哦!该怎么做呢??!!