Hi guys,
quick question. Does the structure

try
{

}

catch

{

exit(0);
}

require the preprocessor directive

#include<cstdlib>

to be declared at the beginning of the program? The thing is if I don't declare it the visual c++ compiler works fine but if I try in Unix with g++ it doesn't compile...
thanks

Exceptions do not exist in C, so including the stdlib won't do any good.
I don't know the exact details, but maybe Visual C++ accepts 'catch' without any specification as to what you want to catch, and g++ doesn't.

In any case, I suggest you specify what exceptions you want to catch, for example:

try {
   throw 1;
} catch( int e )
{ 

}

try {
    throw std::something;
} catch( std::exception& e )
{

}

try {

} catch( ... )  // Catches every C++ exception
{

}

Edited 6 Years Ago by thelamb: typo

Hi guys,
quick question. Does the structure

try
{

}

catch

{

exit(0);
}

require the preprocessor directive

#include<cstdlib>

to be declared at the beginning of the program? The thing is if I don't declare it the visual c++ compiler works fine but if I try in Unix with g++ it doesn't compile...
thanks

You only need cstdlib for exit(0);

hi thanks, yeah that's what I thought, but I was not sure. SO the try catch itself is fine but when I get

exit(0);

I need cstdlib. Cool, thanks guys

Edited 6 Years Ago by Violet_82: n/a

This question has already been answered. Start a new discussion instead.