Description
This example shows how to prevent the compiler from throwing an exception
and how to generate and display the same exception by our own code.
Form1 contains a Button.
UserDefinedExceptions.cpp UserDefinedExceptions.h Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.
#include <vcl.h>
#include <iostream.h>
#pragma hdrstop
#include "UserDefinedExceptions.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
//Global variable
bool Throw;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Throw = false;
}
//---------------------------------------------------------------------------
class DivisionError
{
private:
public:
//CONSTRUCTOR
DivisionError()
{
}
void Response(); //Response function
};
//--------------------------------------------------------------------
void DivisionError::Response()
{
//Respond to the user defined exception by displayoing an error
//message and terminating the program.
ShowMessage("DivisionError Response(): Attempt to divide by 0");
Application->Terminate();
}
//--------------------------------------------------------------------
class Division
{
private:
double numerator;
double denominator;
public:
Division ( double n, double d ); //CONSTRUCTOR WITH TWO ARGUMENTS
double DoDivision(); //Method to perform the division
};
//--------------------------------------------------------------------
Division::Division ( double n, double d )
{
//Division Constructor with two arguments
// Exceptions shouh be thrown from the
// Division Constructor to centralize Error
// Detection
if ( d == 0 )
{
DivisionError DivisionError;
//"throw user defined exception"
Throw = true;
}
numerator = n;
denominator = d;
}
//----------------------------------------------------------------------
double Division::DoDivision()
{
if ( Throw )
{
return 0;
}
else
{
return numerator/denominator;
}
}
//--------------------------------------------------------------------
void DisplayDivision( Division D )
{
//The argument to this function is a Division object
//Show the result of the division in a Box only
//if user defined exception was not generated.
if ( !Throw )
{
ShowMessage( FloatToStr( D.DoDivision() ) );
}
}
//--------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
try
{
Division Division1( 100, 10 ); //Create an object Division1
DisplayDivision( Division1 );
Division Division2( 100, 5 ); //Create an object Division2
DisplayDivision( Division2 );
//Division by zero
Division Division3( 100, 0 ); //Create an object Division1
DisplayDivision( Division3 );
}
catch ( ... )
{
//Place the desired response code for the
//compiler generated exceptions here
}
//"USER DEFINED CATCH"
if ( Throw )
{
//ShowMessage("Throw");
DivisionError Error;
Error.Response();
}
}
#ifndef UserDefinedExceptionsH
#define UserDefinedExceptionsH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------