Details on File Processing
Details on File Processing
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:
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.
#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);
};#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";
}// 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);
}
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) );
}
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) );
}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);
}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();
}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");
}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();
}
}
}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();
}
}
| ||||||||||||||||||||||||||||||||||||||||||