/********************************************\
|????歡迎轉載, 但請保留作者姓名和原文鏈接, 祝您進步并共勉!???? |
\********************************************/
C++異常處理一例
作者: Jerry Cat
時間: 2006/05/30
鏈接:?http://www.shnenglu.com/jerysun0818/archive/2006/05/30/7851.html
#include<fstream.h>
#include<iostream.h>
#include<stdlib.h>
void main()
{
??? ifstream source("c:\abc.txt");? //打開文件
??? char line[128];
??? try //定義異常
??? {
??????? if (source.fail())
??????? throw "txt";? //拋擲異常
??? }
??? catch(char * s)?? //定義異常處理
??? {
??????? cout<<"error opening the file "<<s<<endl;
??????? exit(1);
??? }
??? while(!source.eof())
??? {
??????? source.getline(line, sizeof(line));
??????? cout<<line<<endl;}
??????? source.close();
??? }
}
posted on 2006-05-30 01:38
Jerry Cat 閱讀(685)
評論(1) 編輯 收藏 引用