tratamiento de excepciones

konak
05 de Junio del 2004
quien me puede ayudar con las excepciones. o sea un pequeno ejemplo de como usr el try catch

noel solw
05 de Junio del 2004
Aqui va un ejemplo del tratamiento de excepciones .
Espero que te sea util.


// program k17a1.cpp - page 352
// exceptions : StringMultiplication().
//written in microsoft visual c++ - version 6.0

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
//-------------------------------------------------------------------
char *StringMultiplication(const char *str,int n)
{
cout << str << "* " << n << " = ";
try
{
if(!str[0])
throw("(* empty string *)");
if(n <= 0)
throw("(* n must be positive integer *)");
char *p = new char[strlen(str)*n + 1];
if(!p)
throw("(* memory allocation error *)");
p[0] = 0;
for(int i = 0;i < n;i++)
strcat(p,str);
return p;
} // TRY
catch(char *msg)
{
return msg;
} // CATCH
} // STRING MULTIPLICATION

void Process()
{
cout << StringMultiplication("juan jose de san martin ",1) << endl;
cout << StringMultiplication("juan ",10) << endl;
cout << StringMultiplication("",10) << endl;
cout << StringMultiplication("jose ",-10) << endl;
cout << StringMultiplication("JuanJoseDeSanMartin ",10) << endl;
} // PROCESS

void main()
{
cout << "exceptions : StringMultiplication(). " << endl;
cout << "-----------------------------------------------------"
"-------------------------n";
Process();
cout << "-----------------------------------------------------"
"-------------------------n";
cout << "end of program - good bye ! ! !n";
} // MAIN