//
异常处理
,try,throw,catch
try{
cout<<"Inside try block/n";
throw 99 ;
cout<<"This will not execute"; // 被 throw 后,所以此句不执行
}
try{
cout<<"Inside try block/n";
throw 99 ;
cout<<"This will not execute"; // 被 throw 后,所以此句不执行
}
catch(int i){
cout<<"Caught an exception--value is: "<<i<<endl; // 得到 throw 扔出的值 99
}
cout<<"Caught an exception--value is: "<<i<<endl; // 得到 throw 扔出的值 99
}
//throw
扔出的值
,
与
catch
得到的值类型匹配
//
被扔出值后,程序就跳到
catch
语句进行处理。处理完后继续执行
catch
后面的语句。
class Myexception{
public:
char str_what[80];
Myexception() { *str_what=0; }
Myexception(char *s) { strcpy(str_what,s); }
};//
定义一个类
Myexception
//
主函数中引用
try{
cout<<"Enter numerator and denominator: ";
cin>>a>>b;
if(!b) throw Myexception("Cannot divide by zero!");
else cout<<"Quotient is: "<<a/b<<endl;
}
catch(Myexception e){
cout<<e.str_what<<endl; } // 由此处引用前面的扔出值
cout<<"Enter numerator and denominator: ";
cin>>a>>b;
if(!b) throw Myexception("Cannot divide by zero!");
else cout<<"Quotient is: "<<a/b<<endl;
}
catch(Myexception e){
cout<<e.str_what<<endl; } // 由此处引用前面的扔出值
//
当有两个类,一个是另一个的派生类,此时,尽管扔出值可能是派生类,但是
catch
时定义的
//
基类也能接受,所以如果同时定义两个接受的类型,在前面的
catch
语句执行,将跳过后面的
catch(...){ }//
必须位于所有
catch
语句的后面。
//
另一种定义
throw
的类型确定
void Xhandler(int test) throw(int, char ,double)
{
if(test==0) throw test;
if(test==1) throw 'a';
if(test==2) throw 123.23;
}
void Xhandler(int test) throw(int, char ,double)
{
if(test==0) throw test;
if(test==1) throw 'a';
if(test==2) throw 123.23;
}
//
用于判断是否分配空间成功
try{
p=new int[32];//include <new>
}
catch(bad_alloc xa){
cout<<"Allocation failure./n";
return 1;
}//
最后需要
delete []p;
释放空间
//
另一种判断方法
p=new(nothrow) int[32];
if(!p){
cout<<"Allocation failure./n";
return 1;
}
//
类中定义带有异常处理得
new
void *three_d::operator new(size_t size)
{
void *p;
cout<<"Allocating three_d object./n";
p=malloc(size);
if(!p){
bad_alloc ba;
throw ba;
}
return p;
}
void *three_d::operator new(size_t size)
{
void *p;
cout<<"Allocating three_d object./n";
p=malloc(size);
if(!p){
bad_alloc ba;
throw ba;
}
return p;
}
//I/O
系统
ostream &operator<<( ostream &stream, three_d obj )
{
stream << obj.x << ",";
stream << obj.y << ",";
stream << obj.z << "/n";
return stream;
}//
重新释义
iostream
库中的的输出运算符
ostream<<
//
可以在类中定义友元
friend ostream &operator<<( ostream &stream, three_d obj);
istream &operator>>( istream &stream, three_d &obj)
{
cout<<"Enter x,y,z values: ";
stream >> obj.x >> obj.y >> obj.z;
return stream;
} // 重新释义 iostream 库中的的输出运算符 istream>>
{
cout<<"Enter x,y,z values: ";
stream >> obj.x >> obj.y >> obj.z;
return stream;
} // 重新释义 iostream 库中的的输出运算符 istream>>
//
说
明格式化
I/O
cout.setf( ios::showpos );
//
显示
+-
号
cout.setf( ios::scientific ); // 科学输入法
cout.setf( ios::scientific ); // 科学输入法
ios::fmtflags f;//
标准
IO
库中的
ios
类
cout.precision(2);
cout.width(10);
cout.width(10);
cout.fill('#');
#include <iomanip>
stream.setf(ios::right);
stream<<setw(10)<<setfill('$');
stream<<setw(10)<<setfill('$');
//
写入一个文件
--12
#include <iostream>
#include <fstream>
using namespace std;
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream out("test ");
// 创建 test 文件 . ofstream out("test .txt "); 创建的即为 test.txt 文本文件
if(!out)
{
cout<<"cannot open file./n";
return 1;
}
out<<10<<" "<<123.23<<endl;
out<<"this is a short text file."; //test 文件中写入 10,123.23,this is a short text file
out.close();
return 0;
}
{
ofstream out("test ");
// 创建 test 文件 . ofstream out("test .txt "); 创建的即为 test.txt 文本文件
if(!out)
{
cout<<"cannot open file./n";
return 1;
}
out<<10<<" "<<123.23<<endl;
out<<"this is a short text file."; //test 文件中写入 10,123.23,this is a short text file
out.close();
return 0;
}
int main()
{
char ch;
int i;
float f;
char str[80];
{
char ch;
int i;
float f;
char str[80];
ifstream in("test");
if(!in)
{
cout<<"cannot open file./n";
return 1;
}
if(!in)
{
cout<<"cannot open file./n";
return 1;
}
in>>i;
in>>f;
in>>ch;
in>>str; // 从 test 文件中读数据到 i,f,ch,str
cout<<i<<" "<<f<<" "<<ch<<endl;
cout<<str<<endl;
in.close();
return 0;
}
in>>f;
in>>ch;
in>>str; // 从 test 文件中读数据到 i,f,ch,str
cout<<i<<" "<<f<<" "<<ch<<endl;
cout<<str<<endl;
in.close();
return 0;
}
//get()
输
出文件内容
--14
#include <iostream>
#include <fstream>
using namespace std;
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
char ch;
argc=2; argv[1]="test";
{
char ch;
argc=2; argv[1]="test";
if(argc!=2)
{
cout<<"Usage: PR <filename>/n";
return 1;
}
{
cout<<"Usage: PR <filename>/n";
return 1;
}
ifstream in(argv[1], ios::in | ios::binary);
if(!in)
{
cout<<"cannot open file./n";
return 1;
}
if(!in)
{
cout<<"cannot open file./n";
return 1;
}
while(in)
{
in.get(ch);
if(in)
cout<<ch;
}
{
in.get(ch);
if(in)
cout<<ch;
}
in.close();
return 0;
}
}
//put()
输
入文件内容
--15
#include <iostream>
#include <fstream>
using namespace std;
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
char *p="hello there";
{
char *p="hello there";
ofstream out("test", ios::out|ios::binary);
if(!out)
{
cout<<"cannot open file./n";
return 1;
}
if(!out)
{
cout<<"cannot open file./n";
return 1;
}
while(*p)
out.put(*p++);
out.put(*p++);
return 0;
}
}
//read()
和
write()--16
#include <iostream>
#include <fstream>
using namespace std;
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
int n[5]={1,2,3,4,5};
register int i;
{
int n[5]={1,2,3,4,5};
register int i;
ofstream out("test", ios::out|ios::binary);
if(!out)
{
cout<<"cannot open file./n";
return 1;
}
if(!out)
{
cout<<"cannot open file./n";
return 1;
}
out.write( (char *)&n, sizeof n );
out.close();
out.close();
for(i=0; i<5; i++)
n[i]=0;
n[i]=0;
ifstream in("test", ios::in|ios::binary );
if(!in)
{
cout<<"cannot open file./n";
return 1;
}
if(!in)
{
cout<<"cannot open file./n";
return 1;
}
in.read( (char *)&n, sizeof n );
for(i=0; i<5; i++)
cout<<n[i]<<" ";
cout<<n[i]<<" ";
in.close();
return 0;
}
}