清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
C++异常处理的几种方法#include<cstdio> class BadSrcFile{}; class BadDestFile{}; class BadCopy{}; int myCp(const char* destFile,const char* srcFile) { FILE* inFile; if((inFile=fopen(srcFile,"rb"))==0) { return 1; } FILE* outFile; if((outFile=fopen(destFile,"wb"))==0) { return 2; } char buf[20]; size_t inSize; size_t outSize; while((inSize=fread(buf,sizeof(char),20,inFile))!=0) { outSize=fwrite(buf,sizeof(char),inSize,outFile); if(outSize!=inSize) { return 3; } } fclose(inFile); fclose(outFile); return 0; } int myCp2(const char* destFile,const char* srcFile) { FILE* inFile; if((inFile=fopen(srcFile,"rb"))==0) { // return 1; throw 1; } FILE* outFile; if((outFile=fopen(destFile,"wb"))==0) { // return 2; throw 2; } char buf[20]; size_t inSize; size_t outSize; while((inSize=fread(buf,sizeof(char),20,inFile))!=0) { outSize=fwrite(buf,sizeof(char),inSize,outFile); if(outSize!=inSize) { // return 3; throw 3; } } fclose(inFile); fclose(outFile); return 0; } void myCp3(const char* destFile,const char* srcFile) { FILE* inFile; if((inFile=fopen(srcFile,"rb"))==0) { // return 1; throw "打开输入文件时出错!\n"; } FILE* outFile; if((outFile=fopen(destFile,"wb"))==0) { // return 2; throw "打开输出文件时出错!\n"; } char buf[20]; size_t inSize; size_t outSize; while((inSize=fread(buf,sizeof(char),20,inFile))!=0) { outSize=fwrite(buf,sizeof(char),inSize,outFile); if(outSize!=inSize) { // return 3; throw "拷贝时出错!\n"; } } fclose(inFile); fclose(outFile); } void myCp4(const char* destFile,const char* srcFile) { FILE* inFile; if((inFile=fopen(srcFile,"rb"))==0) { // return 1; //throw "打开输入文件时出错!\n"; throw BadSrcFile(); } FILE* outFile; if((outFile=fopen(destFile,"wb"))==0) { // return 2; // throw "打开输出文件时出错!\n"; throw BadDestFile(); } char buf[20]; size_t inSize; size_t outSize; while((inSize=fread(buf,sizeof(char),20,inFile))!=0) { outSize=fwrite(buf,sizeof(char),inSize,outFile); if(outSize!=inSize) { // return 3; // throw "拷贝时出错!\n"; throw BadCopy(); } } fclose(inFile); fclose(outFile); } int main() { int result= myCp("11.txt","1.txt"); int result1; switch(result) { case 0:printf("正常拷贝1!\n");break; case 1:printf("打开输入文件时出错1!\n");break; case 2:printf("打开输出文件时出错1!\n");break; case 3:printf("拷贝时出错1!\n");break; default:printf("未知错误1!\n");break; } try{ result=myCp2("22.txt","1.txt"); myCp3("33.txt","1.txt"); myCp4("44.txt","1.txt"); } catch(int e){ switch(e) { case 1:printf("打开输入文件时出错2!\n");break; case 2:printf("打开输出文件时出错2!\n");break; case 3:printf("拷贝时出错2!\n");break; default:printf("未知错误2!\n");break; } } catch(const char* e) { printf("%s\n",e); } catch(BadSrcFile e) { printf("打开输入文件时出错!\n"); } catch(BadDestFile e) { printf("打开输出文件时出错!\n"); } catch(BadCopy e) { printf("拷贝时出错!\n"); } catch(...) { printf("未知错误!\n"); } printf("OK!\n"); return 0; }