delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/16/23:10:26

From: d DOT vrabel AT n-shropshire DOT ac DOT uk (David Vrabel)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: does new throws exceptions?
Date: Mon, 15 Jun 1998 07:49:44 GMT
Organization: Staffordshire University
Lines: 51
Message-ID: <3584d14d.1148262@192.168.1.250>
References: <35840cf4 DOT 12845170 AT news DOT plex DOT nl>
NNTP-Posting-Host: www.n-shropshire.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019