Saturday, January 27, 2007

This is a sample ODC error catching file

This is a basic error catching class. What this program does is catch a error and reports it to a designated file.

Two methods are used in this class:

ErrorFound: This method catches the error

Report: This method writes the error to a file This generic class can be put in any program and the actual error declaration is handled in the "main.cpp".

Here's a sample program:


// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include "stdafx.h"
#include "ODC.h"

using namespace System;

int _tmain()
{
ODC* tester = __gc new ODC();
Console::Write(S"Menu\n\n1. Press one\n2. Press two\n\nEnter here: ");

int choice;

choice = Int32::Parse(Console::ReadLine());

if(choice == 1)
Console::Write(S"You entered 1");
else if(choice == 2)
Console::Write(S"You entered 2");
else//error catcher

{
Console::Write(S"Error");
tester->Report(S"User entered wrong number at Main Menu");
}

return 0;
}

Here's the actual .h class that catches the error


using namespace System;
using namespace System::IO;

public __gc class ODC

{
public:
ODC()//constructor
{
file = __gc new StreamWriter(S"C:\\Dell\\Game.log");
}

void ErrorFound(String* e)
{
error = e;
}

void Report(String* e)
{
error = e;
file->WriteLine(error);
file->Close();
}

private:
String* error;
StreamWriter* file;

};

No comments: