Article ID: 157073
Article Last Modified on 11/21/2006
void Test1()
{
try {
//Drive A: should contain a floppy with zero bytes free
CFile file("A:\\BadFile.tmp", CFile::modeCreate
| CFile::shareExclusive | CFile::modeWrite );
try {
CArchive archive(&file, CArchive::store);
//Fill up disk, and cause a
//CFileException::diskFull exception
while(1)
archive << (DWORD)17;
}
catch ( CFileException* ex ) {
ex->Delete();
}
catch (...) {
ASSERT(FALSE);
}
}
catch ( CFileException* ex ) {
ex->Delete();
}
catch (...){
ASSERT(FALSE);
}
}
In this case, if the disk is full, the exception is not caught, terminate()
is called, and the application exits.
// This example shows placing CFile, CArchive, and writes in separate
// try blocks
/* Compile options needed: default AppWizard application
*/
void Test2()
{
try {
//Drive A: should contain a floppy with zero bytes free
CFile file("A:\\BadFile.tmp", CFile::modeCreate
| CFile::shareExclusive | CFile::modeWrite );
try {
CArchive archive(&file, CArchive::store);
try {
//Fill up disk, and cause a
// CFileException::diskFull exception
while(1)
archive << (DWORD)17;
}
catch ( CFileException* ex ) {
archive.Abort();
ex->Delete();
}
catch (...){
ASSERT(FALSE);
}
}
catch ( CFileException* ex ) {
file.Abort();
ex->Delete();
}
catch (...){
ASSERT(FALSE);
}
}
catch ( CFileException* ex ) {
ex->Delete();
}
catch (...){
ASSERT(FALSE);
}
}
// This example uses CArchive::bNoFlushOnDelete so that CFile and
// CArchive can be placed in the same try block.
/* Compile options needed: default AppWizard application
*/
void Test3()
{
try {
//Drive A: should contain a floppy with zero bytes free
CFile file("A:\\BadFile.tmp",
CFile::modeCreate | CFile::shareExclusive
| CFile::modeWrite );
CArchive archive(&file,
CArchive::store | CArchive::bNoFlushOnDelete);
try {
//Fill up disk, and cause a CFileException::diskFull
//exception
while(1)
archive << (DWORD)17;
//Manually flush archive in try-block
archive.Flush();
}
catch ( CFileException* ex ) {
archive.Abort();
ex->Delete();
}
catch (...){
ASSERT(FALSE);
}
}
catch ( CFileException* ex ) {
ex->Delete();
}
catch (...){
ASSERT(FALSE);
}
}
Additional query words: 2.00 2.10 2.20 4.00 4.10 4.20 kbdsd CArchive CFile Exception
Keywords: kbexcepthandling kbfileio kbprb KB157073