I am writing a program using gcc in c++. On dec. 5th I posted a question because of a weird problem ( https://stackoverflow.com/a/8392854/837803 ). When a software exception leaves a function declared with no ‘throw’ specifications like this:
int foo();
or this:
int foo() throw();
gcc-generated code will crash.
I want to tell gcc that any kind of software exception could leave any function I write. I think it is something like:
int foo() throw(...);
BUT: I don’t want to write throw(…) in all function specifications. I realise that my program size will be bigger, but that is not a problem for this case. Also, I have read that the behaviour of gcc that I am suggesting, is an ANSI violation. But that is no problem either.
Among the many, many, many command-line options of gcc, there must be one that I am looking for, but I haven’t found it yet.
What is the option I am looking for?
I take exception to this:
This is just plain wrong.
A function declared like this:
If you throw an exception it will cause the stack to unwind upto an appropriate catch. IF there is no appropriate catch the program exits (weather the stack unwinds in this case is implementation defined (thus put a catch all in main)).
A function declared like this:
If an exception escapes this function then unexpected() is called. The default action of unexpected is to call abort() which causes the program to exit (Note: you can replace unexpected with a user defined function but it has to either exit or throw an alternative exception that can pass the throw specification (in this case that is not possible)).
The behavior you want is the default behavior (with no throw specification). Throw specifications were an experiment that failed and have thus been deprecated. You should not be using them anyway (apart from no-throw).
So you can get normal working code with exceptions if you define your functions like this:
But it is best to put a catch in main()