Home

Exception Handling in File Processing

 

Finally

So far, to handle exceptions, we were using the try, catch, and throw keywords. These allowed us to perform normal assignments in a try section and then handle an exception, if any, in a catch block. We also mentioned that, when you create a stream, the operating system must allocate resources and dedicate them to the file processing operations. Additional resources may be provided for the object that is in charge of writing to, or reading from, the stream. We also saw that, when the streaming was over, we should free the resources and give them back to the operating system. To do this, we called the Close() method of the variable that was using resources.

More than any other assignment, file processing is in prime need of exception handling. During file processing, there are many things that can go wrong. For this reason, the creation and/or management of streams should be performed in a try block to get ready to handle exceptions that would occur. Besides actually handling exceptions, you can use the finally keyword to free resources.

The finally keyword is used to create a section of an exception. Like catch, a finally block cannot exist by itself. It can be created following a try section. The formula used would be:

try
{
}
finally
{
}

Based on this:

  • The finally section has a body of its own, delimited by its curly brackets
  • Like catch, the finally section is created after the try section
  • Unlike catch, finally never has parentheses and never takes arguments
  • Unlike catch, the finally section is always executed

Because the finally clause always gets executed, you can include any type of code in it but it is usually appropriate to free the resources that were allocated such as those used during streaming. Here is an example:

using namespace System;
using namespace System::IO;

int main()
{
    String ^ NameOfFile = L"Members.clb";
	
    FileStream ^ fstPersons = gcnew FileStream(NameOfFile, FileMode::Create);
    BinaryWriter ^ wrtPersons = gcnew BinaryWriter(fstPersons);
        
    try 
    {
        wrtPersons->Write(L"James Bloch");
        wrtPersons->Write(L"Catherina Wallace");
        wrtPersons->Write(L"Bruce Lamont");
        wrtPersons->Write(L"Douglas Truth");
    }
    finally
    {
        wrtPersons->Close();
        fstPersons->Close();
    }

    return 0;
}

In the same way, you can use a finally section to free resources used when reading from a stream:

using namespace System;
using namespace System::IO;

int main()
{
/*    String ^ NameOfFile = L"Members.clb";
	
    FileStream ^ fstPersons = gcnew FileStream(NameOfFile, FileMode::Create);
    BinaryWriter ^ wrtPersons = gcnew BinaryWriter(fstPersons);
        
    try 
    {
        wrtPersons->Write(L"James Bloch");
        wrtPersons->Write(L"Catherina Wallace");
        wrtPersons->Write(L"Bruce Lamont");
        wrtPersons->Write(L"Douglas Truth");
    }
    finally
    {
        wrtPersons->Close();
        fstPersons->Close();
    }*/

	String ^ NameOfFile = L"Members.clb";
    String ^ strLine = nullptr;
        
    FileStream ^ fstMembers = gcnew FileStream(NameOfFile, FileMode::Open);
    BinaryReader ^ rdrMembers = gcnew BinaryReader(fstMembers);

    try
    {
        strLine = rdrMembers->ReadString();
        Console::WriteLine(strLine);
        strLine = rdrMembers->ReadString();
        Console::WriteLine(strLine);
        strLine = rdrMembers->ReadString();
        Console::WriteLine(strLine);
        strLine = rdrMembers->ReadString();
        Console::WriteLine(strLine);
    }
    finally
    {
        rdrMembers->Close();
        fstMembers->Close();
    }

    return 0;
}

Of course, since the whole block of code starts with a try section, it is used for exception handling. This means that you can add the necessary and appropriate catch section(s) but you don't have to.

Practical LearningPractical Learning: Finally Releasing Resources

  1. Start Microsoft Visual C++ and create a CLR Console Application named IceCream4
  2. To create a new class, on the main menu, click Project -> Add Class...
  3. In the Add Class dialog box, in the Templates list, click C++ Class and click Add
  4. Set the Name to CIceCream
  5. Make sure the Managed check box is selected and click Finish
  6. Change the file as follows:
     
    #pragma once
    
    using namespace System;
    using namespace System::IO;
    
    public ref class CIceCream sealed
    {
        // This is the base price of an ice scream
        // Optional values may be added to it
    public:
        static const double BasePrice = 1.55;
    
        // These arrays are used to build the components 
        // of various ice creams
    private:
        array<String ^> ^ Flavor;
        array<String ^> ^ Container;
        array<String ^> ^ Ingredient;
    
        // Additional factor used to process an ice scream order
        int Scoops;
        double TotalPrice;
    
        // Variables that will hold the user's choice
        // These are declared "globally" so they can be 
        // shared among methods
        int ChoiceFlavor;
        int ChoiceContainer;
        int ChoiceIngredient;
    
        // This default constructor is the best place for 
        // us to initialize the array
    public:
        CIceCream(void);
    };
  7. Access the IceCream.cpp file and change the constructor as follows:
     
    #include "StdAfx.h"
    #include "IceScream.h"
    
    CIceCream::CIceCream(void)
    {
            Flavor = gcnew array<String ^>(10);
            Flavor[0] = L"Vanilla";
            Flavor[1] = L"Cream of Cocoa";
            Flavor[2] = L"Chocolate Chip";
            Flavor[3] = L"Organic Strawberry";
            Flavor[4] = L"Butter Pecan";
            Flavor[5] = L"Cherry Coke";
            Flavor[6] = L"Chocolate Brownies";
            Flavor[7] = L"Caramel Au Lait";
            Flavor[8] = L"Chunky Butter";
            Flavor[9] = L"Chocolate Cookie";
    
            Ingredient = gcnew array<String^>(4);
            Ingredient[0] = L"No Ingredient";
            Ingredient[1] = L"Peanuts";
            Ingredient[2] = L"M & M";
            Ingredient[3] = L"Cookies";
    
            Container = gcnew array<String^>(3);
            Container[0] = L"Cone";
            Container[1] = L"Cup";
            Container[2] = L"Bowl";
    }
  8. In the Class View, right-click CIceCream -> Add -> Add Function
  9. Set the Return Type to void
  10. Set the Function Name to ChooseFlavor and click Finish
  11. In the IceCream.cpp source file, implement the ChooseFlavor method as follows:
     
    // This method requests a flavor from the user and
    // returns the choice
    void CIceCream::ChooseFlavor(void)
    {
        // Make sure the user selects a valid number 
        //that represents a flavor...
        do {
            // In case the user types a symbol that 
            // is not a number
            try {
                Console::WriteLine(L"What type of flavor do you want?");
                for (int i = 0; i < Flavor->Length; i++)
                    Console::WriteLine(L"{0} - {1}", i + 1, Flavor[i]);
                Console::Write(L"Your Choice? ");
    	    ChoiceFlavor = int::Parse(Console::ReadLine());
            }
            catch(FormatException ^) // display an appropriate message
            {
                Console::WriteLine(L"You must enter a valid number "
                                   L"and no other character!");
            }
    
            // If the user typed an invalid number out of the 
            // allowed range
            // let him or her know and provide another chance
            if( (ChoiceFlavor < 1) || (ChoiceFlavor > Flavor->Length) )
                Console::WriteLine(L"Invalid Choice - Try Again!\n");
        } while (ChoiceFlavor < 1 || ChoiceFlavor > Flavor->Length);
    }
  12. In the Class View, right-click CIceCream -> Add -> Add Function
  13. Set the Return Type to void
  14. Set the Function Name to ChooseContainer and click Finish
  15. In the IceCream.cpp source file, implement the ChooseContainer method as follows:
     
    void CIceCream::ChooseContainer(void)
    {
        // Make sure the user selects a valid number that 
        // represents a container
        do {
            // If the user types a symbol that is not a number
            try {
                Console::WriteLine(L"What type of container do you want?");
                for (int i = 0; i < Container->Length; i++)
                     Console::WriteLine(L"{0} - {1}", i + 1, Container[i]);
                Console::Write(L"Your Choice? ");
    	    ChoiceContainer = int::Parse(Console::ReadLine());
            }
            catch(FormatException ^) // display an appropriate message
            {
                Console::WriteLine(L"You must enter a valid "
                                   L"number and no other character!");
            }
    
            // If the user typed an invalid number out of the 
            // allowed range
            // let him or her know and provide another chance
            if( (ChoiceContainer < 1) ||
                (ChoiceContainer > Container->Length) )
                Console::WriteLine(L"Invalid Choice - Try Again!");
        } while( (ChoiceContainer < 1) ||
                 (ChoiceContainer > Container->Length) );
    }
  16. In the Class View, right-click CIceCream -> Add -> Add Function
  17. Set the Return Type to void
  18. Set the Function Name to ChooseIngredient and click Finish
  19. In the IceCream.cpp source file, implement the ChooseIngredient method as follows:
     
    void CIceCream::ChooseIngredient(void)
    {
        do {
            try {
                Console::WriteLine(L"Do you want an ingredient or not");
                for (int i = 0; i < Ingredient->Length; i++)
                    Console::WriteLine(L"{0} - {1}",
                                       i + 1,
                                       Ingredient[i]);
                Console::Write(L"Your Choice? ");
                ChoiceIngredient = int::Parse(Console::ReadLine());
            }
            catch(FormatException ^)
            {
                Console::WriteLine(L"You must enter a valid "
                                   L"number and no other character!");
            }
    
            if( (ChoiceIngredient < 1) ||
                (ChoiceIngredient > Ingredient->Length) )
                Console::WriteLine(L"Invalid Choice - Try Again!");
        } while( (ChoiceIngredient < 1) ||
                 (ChoiceIngredient > Ingredient->Length) );
    }
  20. In the Class View, right-click CIceCream -> Add -> Add Function
  21. Set the Return Type to void
  22. Set the Function Name to SpecifyNumberOfScoops and click Finish
  23. In the IceCream.cpp source file, implement the SpecifyNumberOfScoops method as follows:
     
    void CIceCream::SpecifyNumberOfScoops(void)
    {
        do {
            try {
                Console::Write(L"How many scoops(1, 2, or 3)? ");
                Scoops = int::Parse(Console::ReadLine());
            }
            catch(FormatException ^)
            {
                Console::WriteLine(L"You must enter a valid number "
                                   L"and no other character!");
            }
    
            if( (Scoops < 1) || (Scoops > 3) )
                Console::WriteLine(L"Invalid Choice - Try Again!");
        } while (Scoops < 1 || Scoops > 3);
    }
  24. In the Class View, right-click CIceCream -> Add -> Add Function
  25. Set the Return Type to void
  26. Set the Function Name to ProcessAnOrder and click Finish
  27. In the IceCream.cpp source file, implement the ProcessAnOrder method as follows:
     
    void CIceCream::ProcessAnOrder(void)
    {
        double PriceIngredient, PriceScoop;
    
        // Let the user know that this is a vending machine
        Console::WriteLine(L"=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
        Console::WriteLine(L"Ice Scream Vending Machine");
        Console::WriteLine(L"-----------------------------------");
    
        // Let the user select the components of the ice scream
        ChooseFlavor();
        Console::WriteLine(L"-----------------------------------");
        ChooseContainer();
        Console::WriteLine(L"-----------------------------------");
        ChooseIngredient();
        Console::WriteLine(L"-----------------------------------");
        SpecifyNumberOfScoops();
        Console::WriteLine(L"-----------------------------------");
    
        // If the user selects an ingredient instead of "No Ingredient",
        // add $0.50 to the order
        if( (ChoiceIngredient == 2) ||
            (ChoiceIngredient == 3) ||
            (ChoiceIngredient == 4) )
            PriceIngredient = 0.50;
        else
            PriceIngredient = 0.00;
    
        // Instead of multiplying a number scoops to a value,
        // We will use an incremental value depending on 
        // the number of scoops
        if( Scoops == 1 )
            PriceScoop = 0.65;
        else if( Scoops == 2 )
            PriceScoop = 1.05;
        else
            PriceScoop = 1.55;
    
        // Calculate the total price of the ice scream
        TotalPrice = BasePrice + PriceScoop + PriceIngredient;
    
        // Create the ice scream...
    
        // And display a receipt to the user
        DisplayReceipt();
    }
  28. In the Class View, right-click CIceCream -> Add -> Add Function
  29. Set the Return Type to void
  30. Set the Function Name to DisplayReceipt and click Finish
  31. In the IceCream.cpp source file, implement the DisplayReceipt method as follows:
     
    void CIceCream::DisplayReceipt(void)
    {
        Console::WriteLine(L"\n=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=");
        Console::WriteLine(L"Ice Scream Order");
        Console::WriteLine(L"-----------------------------------");
        Console::WriteLine(L"Flavor:      {0}",
                           Flavor[ChoiceFlavor - 1]);
        Console::WriteLine(L"Container:   {0}",
                           Container[ChoiceContainer - 1]);
        Console::WriteLine(L"Ingredient:  {0}",
                           Ingredient[ChoiceIngredient - 1]);
        Console::WriteLine(L"Scoops:      {0}", Scoops);
        Console::WriteLine(L"Total Price: {0:C}", TotalPrice);
        Console::WriteLine(L"=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=\n");
    }
  32. In the Class View, right-click CIceCream -> Add -> Add Function
  33. Set the Return Type to void
  34. Set the Function Name to SaveOrder and click Finish
  35. In the IceCream.cpp source file, implement the SaveOrder method as follows:
     
    void CIceCream::SaveOrder(void)
    {
        String ^ strNameOfFile;
    
        Console::Write(L"Please enter your initials or the name "
                       L"we will use to remember your order: ");
        strNameOfFile = Console::ReadLine();
        strNameOfFile = strNameOfFile + L".icr";
    
        // Find out if the user entered a name of a file 
        // that is already in the machine
    	if( File::Exists(strNameOfFile) )
        {
            String ^ answer;
    
            FileStream ^ stmIceScream =
                        gcnew FileStream(strNameOfFile, FileMode::Create);
            BinaryWriter ^ bnwIceScream =
                        gcnew BinaryWriter(stmIceScream);
    
            try {
                // If so, find out if the user wants to 
                // replace the old file
                Console::WriteLine(L"The file you entered exists already.");
                Console::Write(L"Do you want to replace it(y/n)?");
    			answer = Console::ReadLine();
    
                // If the customer wants to replace it...
                if( (answer == L"y") || (answer == L"Y") )
                {
                    // ... do so
                    Console::WriteLine(L"The former order with the same "
                                       L"name will be replaced");
    
                    Console::WriteLine(
    			L"\n=-= Ice Scream Vending Machine =-=");
                    Console::WriteLine(L" Saving Order: {0}", strNameOfFile);
                    bnwIceScream::Write(Flavor[ChoiceFlavor - 1]);
                    bnwIceScream::Write(Container[ChoiceContainer - 1]);
                    bnwIceScream::Write(Ingredient[ChoiceIngredient - 1]);
                    bnwIceScream::Write(Scoops);
                    bnwIceScream::Write(TotalPrice);
                }
                // If the customer wants to save the new order with 
                // a different name
                else if( (answer == L"n") || answer == L"N") )
                {
                    // Ask the user to enter a name to remember the order
                    Console::Write(L"Please enter a name we will use "
                                   L"to remember this order: ");
                    strNameOfFile = Console::ReadLine();
                    strNameOfFile = strNameOfFile + L".icr";
    
                    stmIceScream = 
    			gcnew FileStream(strNameOfFile, FileMode::Create);
                    bnwIceScream = gcnew BinaryWriter(stmIceScream);
    
                    Console::WriteLine(
    			L"\n=-= Ice Scream Vending Machine =-=");
                    Console::WriteLine(L" Saving Order: {0}", strNameOfFile);
                    bnwIceScream::Write(Flavor[ChoiceFlavor - 1]);
                    bnwIceScream::Write(Container[ChoiceContainer - 1]);
                    bnwIceScream::Write(Ingredient[ChoiceIngredient - 1]);
                    bnwIceScream::Write(Scoops);
                    bnwIceScream::Write(TotalPrice);
                }
                else
                    Console::WriteLine(L"Invalid Answer - We will close");
            }
            finally
            {
                bnwIceScream->Close();
                stmIceScream->Close();
            }
        }
        else
        {
            FileStream ^ stmIceScream =
                        gcnew FileStream(strNameOfFile, FileMode::Create);
            BinaryWriter ^ bnwIceScream =
                        gcnew BinaryWriter(stmIceScream);
    
            try {
                Console::WriteLine(L"\n=-= Ice Scream Vending Machine =-=");
                Console::WriteLine(L" Saving Order: {0}", strNameOfFile);
                bnwIceScream::Write(Flavor[ChoiceFlavor - 1]);
                bnwIceScream::Write(Container[ChoiceContainer - 1]);
                bnwIceScream::Write(Ingredient[ChoiceIngredient - 1]);
                bnwIceScream::Write(Scoops);
                bnwIceScream::Write(TotalPrice);
            }
            finally
            {
                bnwIceScream->Close();
                stmIceScream->Close();
            }
        }
    }
  36. In the Class View, right-click CIceCream -> Add -> Add Function
  37. Set the Return Type to void
  38. Set the Function Name to OpenOrder and click Finish
  39. In the IceCream.cpp source file, implement the OpenOrder method as follows:
     
    void CIceCream::OpenOrder(void)
    {
        String ^ strNameOfFile;
        String ^ SelectedFlavor;
        String ^ SelectedContainer;
        String ^ SelectedIngredient;
    
        // Ask the user to enter a name of a previously saved order
        Console::Write(L"Please enter the name you previously "
                       L"gave to remember your order: ");
        strNameOfFile = Console::ReadLine();
        strNameOfFile = strNameOfFile + L".icr";
    
        FileStream ^ stmIceScream =
                    gcnew FileStream(strNameOfFile, FileMode::Open);
        BinaryReader ^ bnrIceScream =
                    gcnew BinaryReader(stmIceScream);
    
        try {
            // Find out if this order was previously saved in the machine
    		if( File::Exists(strNameOfFile) )
            {
                // If so, open it
                SelectedFlavor = bnrIceScream->ReadString();
                SelectedContainer = bnrIceScream->ReadString();
                SelectedIngredient = bnrIceScream->ReadString();
                Scoops = bnrIceScream->ReadInt32();
                TotalPrice = bnrIceScream->ReadDouble();
    
                // And display it to the user
                Console::WriteLine(L"\n=-= Ice Scream Vending Machine =-=");
                Console::WriteLine(L" Previous Order: {0}", strNameOfFile);
                Console::WriteLine(L"Flavor:      {0}", SelectedFlavor);
                Console::WriteLine(L"Container:   {0}", SelectedContainer);
                Console::WriteLine(L"Ingredient:  {0}", SelectedIngredient);
                Console::WriteLine(L"Scoops:      {0}", Scoops);
                Console::WriteLine(L"Total Price: {0:C}\n", TotalPrice);
            }
            else
                Console::WriteLine(L"The name you entered is not "
                                   L"registered in our previous orders");
        }
        finally
        {
            bnrIceScream->Close();
            stmIceScream->Close();
        }  
    }
  40. Access the IceCream4.cpp source file and change it as follows:
     
    // IceCream4.cpp : main project file.
    
    #include "stdafx.h"
    #include "IceCream.h"
    
    using namespace System;
    
    int main()
    {
        String ^ answer = L"n";
        CIceCream ^ ic = gcnew CIceCream();
    
        Console::Write(L"Do you want to re-order a previously "
                       L"saved order(y/n)? ");
        answer = Console::ReadLine();
    
        if( (answer == L"y") || (answer == L"Y") )
            ic->OpenOrder();
        else
        {
            ic->ProcessAnOrder();
            Console::Write(L"Do you want us to remember this "
                           L"order the next time you come to "
                           L"get your ice scream (y/n)? ");
            answer = Console::ReadLine();
            if( (answer == L"y") || (answer == L"Y") )
                ic->SaveOrder();
            else
                Console::WriteLine(L"\nIt was nice serving you."
                                   L"\nCome Again!!!\n");
        }
    
        return 0;
    }
  41. Execute the application and test it. Here is an example:
     
    Do you want to re-order a previously saved order(y/n)? n
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    Ice Scream Vending Machine
    -----------------------------------
    What type of flavor do you want?
    1 - Vanilla
    2 - Cream of Cocoa
    3 - Chocolate Chip
    4 - Organic Strawberry
    5 - Butter Pecan
    6 - Cherry Coke
    7 - Chocolate Brownies
    8 - Caramel Au Lait
    9 - Chunky Butter
    10 - Chocolate Cookie
    Your Choice? 6
    -----------------------------------
    What type of container do you want?
    1 - Cone
    2 - Cup
    3 - Bowl
    Your Choice? 2
    -----------------------------------
    Do you want an ingredient or not
    1 - No Ingredient
    2 - Peanuts
    3 - M & M
    4 - Cookies
    Your Choice? 1
    -----------------------------------
    How many scoops(1, 2, or 3)? 2
    -----------------------------------
    
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    Ice Scream Order
    -----------------------------------
    Flavor:      Cherry Coke
    Container:   Cup
    Ingredient:  No Ingredient
    Scoops:      2
    Total Price: $2.60
    =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
    
    Do you want us to remember this order the next time you 
    come to get your ice scream (y/n)? n
    
    It was nice serving you.
    Come Again!!!
    
    Press any key to continue . . .
  42. Close the DOS window
  43. Execute the application and test it. Enter a wrong file name. Here is an example:
     
    Do you want to re-order a previously saved order(y/n)? y
    Please enter the name you previously gave to remember your order: dtr
    
    Unhandled Exception: System::IO.FileNotFoundException: 
    	Could not find file 'C:\Do
    cuments and Settings\Administrator\My Documents\
    	Visual Studio 2005\Projects\Vend
    ingMachine4\IceCream4\bin\Release\dtr.icr'.
    File name: 'C:\Documents and Settings\Administrator\
    	My Documents\Visual Studio 2
    005\Projects\VendingMachine4\IceCream4\bin\Release\dtr.icr'
       at System::IO.__Error.WinIOError(Int32 errorCode, String ^ maybeFullPath)
       at System::IO.FileStream.Init(String ^ path, 
    	FileMode mode, FileAccess access, I
    nt32 rights, Boolean useRights, FileShare share, 
    	Int32 bufferSize, FileOptions o
    ptions, SECURITY_ATTRIBUTES secAttrs, String ^ msgPath, Boolean bFromProxy)
       at System::IO.FileStream..ctor(String ^ path, FileMode mode)
       at IceCream4.IceScream.OpenOrder() in 
    	C:\Documents and Settings\Administrator
    \My Documents\Visual Studio 2005\Projects\
    	VendingMachine4\IceCream4\IceScream.cs
    :line 360
       at IceCream4.Program.Main(String[] args) in 
    	C:\Documents and Settings\Adminis
    trator\My Documents\Visual Studio 2005\Projects\
    	VendingMachine4\IceCream4\Progra
    m.cs:line 18
    Press any key to continue . . .
  44. Close the DOS window

.NET Framework Exception Handling for File Processing

In the previous lesson of our introduction to file processing, we behaved as if everything was alright. Unfortunately, file processing can be very strict in its assignments. Fortunately, the .NET Framework provides various Exception-oriented classes to deal with almost any type of exception you can think of.

One of the most important aspects of file processing is the name of the file that will be dealt with. In some cases you can provide this name to the application or document. In some other cases, you would let the user specify the name of the path. Regardless of how the name of the file would be provided to the operating system, when this name is acted upon, the compiler will be asked to work on the file. If the file doesn't exist, the operation cannot be carried. Furthermore, the compiler would throw an error. There are many other exceptions that can be thrown as a result of something going bad during file processing:

FileNotFoundException: The exception thrown when a file has not been found is of type FileNotFoundException. Here is an example of handling it:

using namespace System;
using namespace System::IO;

int main()
{
/*    String ^ NameOfFile = L"Members.clb";
	
    FileStream ^ fstPersons = gcnew FileStream(NameOfFile, FileMode::Create);
    BinaryWriter ^ wrtPersons = gcnew BinaryWriter(fstPersons);
        
    try 
    {
        wrtPersons->Write(L"James Bloch");
        wrtPersons->Write(L"Catherina Wallace");
        wrtPersons->Write(L"Bruce Lamont");
        wrtPersons->Write(L"Douglas Truth");
    }
    finally
    {
        wrtPersons->Close();
        fstPersons->Close();
    }*/
    
    String ^ NameOfFile = L"Members.clc";
    String ^ strLine = L"";

    try {
        FileStream ^ fstMembers =
                gcnew FileStream(NameOfFile, FileMode::Open);
        BinaryReader ^ rdrMembers = gcnew BinaryReader(fstMembers);

        try {
            strLine = rdrMembers->ReadString();
            Console::WriteLine(strLine);
			strLine = rdrMembers->ReadString();
            Console::WriteLine(strLine);
            strLine = rdrMembers->ReadString();
            Console::WriteLine(strLine);
            strLine = rdrMembers->ReadString();
            Console::WriteLine(strLine);
        }
        finally
        {
            rdrMembers->Close();
            fstMembers->Close();
        }
    }
    catch (FileNotFoundException ^ ex)
    {
        Console::Write(L"Error: " + ex->Message);
        Console::WriteLine(L" May be the file doesn't exist " +
                           L"or you typed it wrong!");
    }

    return 0;
}

Here is an example of what this would produce:

Error: Could not find file 'c:\Documents and Settings\Administrator
\My Documents\Visual Studio 2005\Projects\FileProcessing2
\FileProcessing2\Members.clc'. May be the file doesn't exist 
or you typed it wrong!
Press any key to continue . . .

IOException: As mentioned already, during file processing, anything could go wrong. If you don't know what caused an error, you can throw the IOException exception.

Practical LearningPractical Learning: Handling File Processing Exceptions

  1. In the Scopes combo box, select CIceCream
  2. In the Functions combo box, select SaveOrder
  3. To throw exceptions, change the method as follows:
     
    void CIceCream::SaveOrder(void)
    {
        String ^ strNameOfFile;
    
        Console::Write(L"Please enter your initials or the name "
                       L"we will use to remember your order: ");
        strNameOfFile = Console::ReadLine();
        strNameOfFile = strNameOfFile + L".icr";
    
        try {
            // Find out if the user entered a name of a file 
            // that is already in the machine
            if( File::Exists(strNameOfFile) )
            {
                String ^ answer;
                FileStream ^ stmIceScream =
                        gcnew FileStream(strNameOfFile, FileMode::Create);
                BinaryWriter ^ bnwIceScream =
                        gcnew BinaryWriter(stmIceScream);
    
                try {
                    // If so, find out if the user wants to 
                    // replace the old file
                    Console::WriteLine(L"The file you entered exists already.");
                    Console::Write(L"Do you want to replace it(y/n)?");
    		        answer = Console::ReadLine();
    
                    // If the customer wants to replace it...
                    if( (answer == "y") || (answer == "Y") )
                    {
                        // ... do so
                        Console::WriteLine(L"The former order with the same "
                                           L"name will be replaced");
    
                        Console::WriteLine(
    			L"\n=-= Ice Scream Vending Machine =-=");
                        Console::WriteLine(L" Saving Order: {0}", strNameOfFile);
                        bnwIceScream->Write(Flavor[ChoiceFlavor - 1]);
                        bnwIceScream->Write(Container[ChoiceContainer - 1]);
                        bnwIceScream->Write(Ingredient[ChoiceIngredient - 1]);
                        bnwIceScream->Write(Scoops);
                        bnwIceScream->Write(TotalPrice);
                    }
                    // If the customer wants to save the new order with 
                    // a different name
                    else if( (answer == "n") || (answer == "N") )
                    {
                        // Ask the user to enter a name to remember the order
                        Console::Write(L"Please enter a name we will use "
                                       L"to remember this order: ");
                        strNameOfFile = Console::ReadLine();
                        strNameOfFile = strNameOfFile + L".icr";
    
                       try {
                            stmIceScream = 
    			    gcnew FileStream(strNameOfFile, FileMode::Create);
                            bnwIceScream = gcnew BinaryWriter(stmIceScream);
    
                            Console::WriteLine(
    				L"\n=-= Ice Scream Vending Machine =-=");
                            Console::WriteLine(
    				L" Saving Order: {0}", strNameOfFile);
                            bnwIceScream->Write(Flavor[ChoiceFlavor - 1]);
                            bnwIceScream->Write(Container[ChoiceContainer - 1]);
                            bnwIceScream->Write(
    				Ingredient[ChoiceIngredient - 1]);
                            bnwIceScream->Write(Scoops);
                            bnwIceScream->Write(TotalPrice);
                        }
                        catch(IOException ^)
                        {
                            Console::Write(L"\nThe file you wanted us to "
    				       L"create exists already. ");
                            Console::WriteLine(L"In case it was registered "
                      			   L"by a different customer, "
    				           L"we will not delete it.");
                        }
                    }
                    else
                        Console::WriteLine(L"Invalid Answer - We will close");
                }
                catch(IOException ^)
                {
                    Console::Write(L"\nThat file exists already. ");
                    Console::WriteLine(L"We need to preserve it just in "
    				 L"case another customer will require it.");
                }
                finally
                {
                    bnwIceScream->Close();
                    stmIceScream->Close();
                }
            }
            else
            {
                FileStream ^ stmIceScream =
                        gcnew FileStream(strNameOfFile, FileMode::Create);
                BinaryWriter ^ bnwIceScream =
                        gcnew BinaryWriter(stmIceScream);
    
                try {
                    Console::WriteLine(L"\n=-= Ice Scream Vending Machine =-=");
                    Console::WriteLine(L" Saving Order: {0}", strNameOfFile);
                    bnwIceScream->Write(Flavor[ChoiceFlavor - 1]);
                    bnwIceScream->Write(Container[ChoiceContainer - 1]);
                    bnwIceScream->Write(Ingredient[ChoiceIngredient - 1]);
                    bnwIceScream->Write(Scoops);
                    bnwIceScream->Write(TotalPrice);
                }
                finally
                {
                    bnwIceScream->Close();
                    stmIceScream->Close();
                }
            }
        }
        catch(IOException ^ ex)
        {
            Console::WriteLine(L"\nError: " + ex->Message);
            Console::WriteLine(L"Operation Canceled: The file you want "
                               L"to create exists already.");
        }
    }
  4. In the Functions combo box, select OpenOrder
  5. To throw exceptions, change the method as follows:
     
    void CIceCream::OpenOrder(void)
    {
        String ^ strNameOfFile;
        String ^ SelectedFlavor;
        String ^ SelectedContainer;
        String ^ SelectedIngredient;
    
        // Ask the user to enter a name of a previously saved order
        Console::Write(L"Please enter the name you previously "
                       L"gave to remember your order: ");
        strNameOfFile = Console::ReadLine();
        strNameOfFile = strNameOfFile + ".icr";
    
        try {
            FileStream ^ stmIceScream =
                    gcnew FileStream(strNameOfFile, FileMode::Open);
            BinaryReader ^ bnrIceScream =
                    gcnew BinaryReader(stmIceScream);
    
            try {
                // Find out if this order was previously saved in the machine
                if( File::Exists(strNameOfFile) )
                {
                    // If so, open it
                    SelectedFlavor = bnrIceScream->ReadString();
                    SelectedContainer = bnrIceScream->ReadString();
                    SelectedIngredient = bnrIceScream->ReadString();
                    Scoops = bnrIceScream->ReadInt32();
                    TotalPrice = bnrIceScream->ReadDouble();
    
                    // And display it to the user
                    Console::WriteLine(L"\n=-= Ice Scream Vending Machine =-=");
                    Console::WriteLine(L" Previous Order: {0}", strNameOfFile);
                    Console::WriteLine(L"Flavor:      {0}", SelectedFlavor);
                    Console::WriteLine(L"Container:   {0}", SelectedContainer);
                    Console::WriteLine(L"Ingredient:  {0}", SelectedIngredient);
                    Console::WriteLine(L"Scoops:      {0}", Scoops);
                    Console::WriteLine(L"Total Price: {0:C}\n", TotalPrice);
                }
                else
                    Console::WriteLine(L"The name you entered is not "
                                       L"registered in our previous orders");
            }
            finally
            {
                bnrIceScream->Close();
                stmIceScream->Close();
            }
        } 
        catch(FileNotFoundException ^ ex)
        {
            Console::Write(L"\nError: " + ex->Message);
            Console::WriteLine(L"It is unlikely that the file exists in "
                      		   L"our machine's register.");
        }
    }
  6. Exercise the application and test it
  7. Close the DOS window
 
 

Previous Copyright © 2004-2007 FunctionX, Inc. Next