tags:

views:

41

answers:

1

i am using the Borland c++ 3.1 compiler. I want to work with exceptions, i've written the following code:

void main (void) {
    int a = 0;
    int b = 1;
    int c;
    try {
        throw 1;
    }
    catch(int a) {
        b = a;
    }
}

The compiler returns a syntax error. what's wrong?

+1  A: 

Most compilers will issue an error stating that your main function must return an int. The main function must return int in a C++ program. It's unsafe to return void from a main function and many modern compilers won't compile. Aside from that everything looks compilable

Raphael
`void` functions don't return a value.
Ruel
He has declared main to be void and the compiler would only warn about that at best I think.
Robert Massaioli
The main function must return int. It's unsafe to return void from a main function and many modern compilers won't compile
Raphael
@Robert and Raul -- Comeau: http://www.comeaucomputing.com/tryitout/ Codepad: http://codepad.org/KGRIwVDm Declaring main as returning anything other than an int is an error, even if some compilers don't issue it.
PigBen
+1 this is true, never use void main() even if your compiler lets you get away with it.
Alexander Rafferty
I agree that it is an error and I would not write my main that way, but is it the error that the poster is getting? I don't think it is.
Robert Massaioli
Borland C++ 3.1 is ancient, around 1992 according to Wikipedia. I'm amazed that it even has exceptions. It certainly doesn't qualify as "modern".
Mark Ransom
Ironically though, you don't actually have to explicitly return a value from main, even though you do have to declare it as type int.
PigBen
PigBen - I believe the compiler adds a 'return 0;' automatically.
Raphael
Robert Massaioli - I just tested on gcc g++ and it gave me an error because of the return type of main. Though I agree that an old compiler would most likely compile it anyway
Raphael