CFileException

  • Post author:
  • Post category:其他



原文地址:

http://www.cnblogs.com/JiMuStudio/archive/2011/07/17/2108483.html

 try 
{ 
CFile   file; 
if   (   !file.Open(_T( "C:/text.TXT "),CFile::modeReadWrite)) 
{ 
  //       ==========> 这段代码写在DLL中上面这个OPEN失败,,直接写在EXE程序中为正确的。。 
} 
  //         ………………………… 
} 
catch(CFileException*   e) 
{ 
      MessageBox( "File   Operation   Error! "); 
} 

是啊,你不是有“catch(CFileException*   e)”吗?错误信息在“e”里面

e-> m_cause是错误值,用ErrLookup可以查看。或者直接在代码里“e-> ReportError()”看看是什么错!


  • CFileException::none

    No error occurred.

  • CFileException::generic

    An unspecified error occurred.

  • CFileException::fileNotFound

    The file could not be located.

  • CFileException::badPath

    All or part of the path is invalid.

  • CFileException::tooManyOpenFiles

    The permitted number of open files was exceeded.

  • CFileException::accessDenied

    The file could not be accessed.

  • CFileException::invalidFile

    There was an attempt to use an invalid file handle.

  • CFileException::removeCurrentDir

    The current working directory cannot be removed.

  • CFileException::directoryFull

    There are no more directory entries.

  • CFileException::badSeek

    There was an error trying to set the file pointer.

  • CFileException::hardIO

    There was a hardware error.

  • CFileException::sharingViolation

    SHARE.EXE was not loaded, or a shared region was locked.

  • CFileException::lockViolation

    There was an attempt to lock a region that was already locked.

  • CFileException::diskFull

    The disk is full.

  • CFileException::endOfFile

    The end of file was reached.


    Note

    These

    CFileException

    cause enumerators are distinct from the

    CArchiveException

    cause enumerators.

//example for CFileException::m_cause
try
{
   CFile f( pFileName, CFile::modeCreate | CFile::modeWrite );
}

catch( CFileException* e )
{
   if( e->m_cause == CFileException::fileNotFound )
      printf( "ERROR: File not found\n")
   e->Delete();
}

Open()























打开或创建文件
Close()






















关闭文件
Read()























无缓冲的数据块读取
Write()






















无缓冲的数据块写入
Flush()






















将缓冲区内的数据写入磁盘
Seek()























移动文件指针
SeekToBegin()
















将文件指针移动到文件头
SeekToEnd()


















将文件指针移动到文件尾
GetLength()


















返回文件长度
SetLength()


















设置文件长度
GetPosititon()
















目前文件指针的位置
Rename()





















更改文件名
Remove()






















删除文件

CFile::modeCreate 创建新文件,如果在创建之前就有同名文件存在,则清除该文件的内容,文件长度变成零.
CFile::modeNoTruncate

必须和CFile::modeCreate合用.它会使得在创建新文件前,若存在同名文件,则直接打开该文件,而不会清除改文件.
CFile::modeRead

打开只读文件,也就是无法将任何数据写入该文件.
CFile::modeReadWrite

打开可读可写的文件.
CFile::modeWrite

打开只能写入的文件.
CFile::modeNoInherit

禁止子进程继承使用此文件
CFile::modeDenyNone

打开文件后,其他执行程序还可以再打开此文件并且读写文件中的数据
CFile::modeDenyRead

打开文件后,其他执行程序可以再次打开此文件,但是只能把数据写入文件
CFile::modeDenyWrite

打开文件后,其他执行程序可以再次打开此文件,但是只能读取文件中的数据
CFile::shareExclusive

打开文件后,禁止其他执行程序再次打开此文件,但是只能把数据写入文件
CFile::typeText

以文本文件打开,也就是CR/LF换行字符组会被解读成CR换行字符
CFile::typeBinary

以二进制模式打开

CException家族成员
CArchiveException

和archive(更高级的文件存取)有关的错误
CDaoException

通过DAO存取数据库的相关错误
CDBException

通过ODBC存取数据库的相关错误
CFileException

和文件存取有关的错误
CInternetException

通过InternetAPI访问网络的相关错误
CMemoryException

内存错误
CNotSupportedException

要求MFC作一项未支持的功能
COleDispatchException

OLE Automation的错误
COleException

OLE相关错误
CResourceException

Windows资源无法创建或搜索不到的错误
CUserException

用户做了一些预料之外的动作

其中在捕捉错误的时候如果觉得太多不多catch很麻烦,也可以仅写一个catch(CException*)来处理所有的错误,但是对于其中的CFileException他比CException多一些记录详细错误信息的成员变量,所以应该加以重视.
CString CFileException::m_strFileName;这个成员变量记录了发生错误的文件名.
int CFileException::m_cause;这个是记录发生错误的原因.
下面是表示错误原因的常数:
CFileException::accessDenied


拒绝存取;无权存取
CFileException::badPath


找不到指定的路径
CFileException::badSeek


无法搜索,移动文件指针
CFileException::directoryFull


目录已满
CFileException::disFull


磁盘已满
CFileException::endOfFile


已经到达文件的结尾处
CFileException::fileNotFound


找不到指定文件
CFileException::generic


一般错误
CFileException::hardIO


硬件发生错误
CFileException::invalidFile


文件无效
CFileException::lockViolation


锁定错误
CFileException::none


正常;无错误
CFileException::removeCurrentDir


所删除的目录是当前的工作目录
CFileException::sharingViolation


共享错误
CFileException::tooManyOpenFiles


已经打开太多的文件

CFileException类的声明文件保存在头文件afx.h中。

当我们在使用CFile及其派生类的对象的时候,如果产生异常则会创建和抛出CFileException对象。采用TRY…CATCH…END_CATCH。

CFileException类的成员变量:

m_cause:错误代码


CFileException::none

没有错误发生


CFileException::generic

一个未被指明的错误发生


CFileException::fileNotFind

该文件不能被定位


CFileException::badPath

整个或者部分路径是无效的


CFileException::tooManyOpenFiles

打开文件的数目太多


CFileException::accessDenied

文件不能被访问


CFileException::invalidFile

试图使用无效文件的句柄


CFileException::removeCurrentDir

当前工作路径不能被移除


CFileException::directoryFull

不再有目录项


CFileException::badSeek

试图设置文件指针错误


CFileException::hardIO

硬件错误


CFileException::sharingViolation

不能调用share.exe文件,或者共享区域被锁


CFileException::lockViolation

试图锁定一个已经被锁的区域


CFileException::diskFull

磁盘空间已满


CFileException::endOfFile

访问到文件尾部

m_IOsEror:操作系统异常错误代码,LONG型

m_strFileName:产生异常情况的文件名称,CString型

CFileException类的成员变量:

CFileException(

int cause = CFileException::none, 异常原因代码

LONG IOsError = -1, 操作系统提示的错误

LPCTSTR lpszArchiveName = NULL 产生错误的CFile对象

);

除了使用全局函数AfxThrowFileException,不能直接创建一个异常文件对象。

注意:IOsError只能应用在CFile和CStdioFile类产生的对象中。CMemFile对象不能操作该错误代码。


static int PASCAL ErrnoToException(int nErrno);

将运行时的错误值转换为一个CFileException被枚举定义的错误值

nErrno:指的是头文件ERRNO.H中定义的运行时错误值

该函数返回与运行时错误相对应的枚举值


static int PASCAL OsErrorToException(LONG IOsError);

将操作系统产生的错误值转换为一个CFileException被枚举定义的错误值

IOsError:指的是操作系统指定的错误值

该函数返回与操作系统错误相对应的枚举值,如果该错误没有对应的CFileException定义的错误值,则会返回

CFileException::generic


static void PASCAL ThrowErrno(


int nErrno,


LPCTSTR lpszFileName = NULL


);

构造一个与ERRNO.H头文件声明的错误值一致的CFileException对象,并抛出该异常。


static void PASCAL ThrowOsError(


LONG IOsError,


LPCTSTR lpszFileName = NULL


);

抛出一个与操作系统错误一致的CFileException对象,如果IOsError错误代码不可知,则抛出异常代码CFileException::generic