Mail Archives: djgpp/1998/06/16/23:10:26
wjae AT plex DOT nl (Wiel Aerts) wrote:
>I tried to use exceptions with the following small piece of
>code. It compiles without problems (gcc 2.8.1).
>
>#include <iostream>
>#include <new>
>
>int main()
>{
> char * p;
> try {
> p = new char[0xFFFFFFF];
> }
> catch (bad_alloc& f) {
> cerr << "wrong: " << f.what() << endl;
> }
> return 0;
>}
>
>Running the program gives the output:
>
>Virtual memory exceeded in `new'
>
>Of course this is correct but the line is not generated
>in the catch block.
>What happens? What I am doing wrong?
Unfortunatly GCC doesn't throw bad_alloc automatically :-(
but you can set it up so it does.
#include<new>
void my_new_handler( void ) { throw bad_alloc(); }
int main( void ) {
set_new_handler( my_new_handler );
...
Check the 'my_new_handler' had the correct return type and parameters
because I can't remember at the moment, and also the set_new_handler
function...
David.
David Vrabel d DOT vrabel AT n-shropshire DOT ac DOT uk
A poor student (aren't they all) at:
North Shropshire College
Oswestry, Shropshire, UK
- Raw text -