--- On Sun, 11/9/08, prakashshukla <[EMAIL PROTECTED]> wrote:

From: prakashshukla <[EMAIL PROTECTED]>
Subject: [c-prog] try catch
To: [email protected]
Date: Sunday, November 9, 2008, 7:52 AM


#include<iostream. h>
I am experimenting with the following program for error handling. I 
get an error on the word try as "unrecognized word". Pl help

#include<conio. h>
void test(int i)
{
try
{if(i!=0) throw i;
else throw "Zero";}
catch(int a)
{cout<<"Caught an int"<<a<<endl; }
catch(char *s)
{ cout<<"Caught a string"<<s<< endl;}
};
void main()
{
clrscr();
test(1);
test(23);
test(O);
getch();
}


You need a "const" before the string catch and you have an O instead of 0 
(zero).
Program below work as it should. I removed the (I think) turbo c++ programming.

#include <cstdlib>
#include <iostream>

using namespace std;

void test(int i)
{
try
{
if(i!=0)
throw i;
else
throw "Zero";
}

catch(int a)
{
cout<<"Caught an int"<<a<<endl; 
}

catch(const char *s)
{ 
cout<<"Caught a string"<<s<< endl;
}

return;

};

int main()
{

test(1);
test(23);
test(0);
system("PAUSE");

return EXIT_SUCCESS;
}


reguards,

Mickey M.
Construction Partner Inc.
http://www.constructionpartner.com










      

[Non-text portions of this message have been removed]

Reply via email to