asm, c, c++ are my all
-- Core In Computer
posts - 139,  comments - 123,  trackbacks - 0

/********************************************\
|    欢迎转载, 但请保留作者姓名和原文链接, 祝您进步并共勉!     |
\********************************************/


C++异常处理一例

作者: Jerry Cat
时间: 2006/05/30
链接: http://www.cppblog.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 阅读(609) 评论(1)  编辑 收藏 引用

FeedBack:
# re: C++异常处理一例
2006-05-30 20:37 | TH
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{

ifstream source("c:\\abc.txt"); //´ò¿ªÎļþ
string line;

try //¶¨ÒåÒì³£
{
if (source.fail())
throw "txt"; //Å×ÖÀÒì³£
}
catch(string& s) //¶¨ÒåÒì³£´¦Àí
{
cout << "error opening the file " << s <<endl;
exit(1);
}

while(!source.eof())
{
getline(source, line, '\n');
cout<< line << endl;
}
source.close();

system("PAUSE");
return EXIT_SUCCESS;
}
  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2006年5月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

常用链接

留言簿(7)

随笔档案

最新随笔

搜索

  •  

最新评论

阅读排行榜

评论排行榜