Re: OT: g++ ignoring try/catch blocks?
On Tue, Dec 27, 2011 at 08:16:00AM -0600, Nate Bargmann wrote:
> I am working through a C++ tutorial and have arrived at the chapter on
> exception handling. The example program sets up a try{} catch() {} set
> of blocks for catching a divide by zero exception. The problem is that
> g++ on Sid is quitting at the divide by zero statement and seemingly
> ignoring the try/catch blocks.
A divide by zero generates a SIGFPE (floating point exception), whose
default action is to immediately terminate the process. It's
important to note that this is /not/ a C++ exception, i.e. a thrown
value, but a UNIX process control signal. In consequence, the
presence of the try/catch blocks has no effect on the process
terminating; the process was killed by the Linux kernel when the
divide by zero was trapped.
See signal(7) and sigaction(2) for more detail. In particular, you
could register a signal handler to ignore the SIGFPE, or to set a
global or thread-local flag which could then be used to throw a C++
exception when a divide by zero occurs.
Regards,
Roger
--
.''`. Roger Leigh
: :' : Debian GNU/Linux http://people.debian.org/~rleigh/
`. `' Printing on GNU/Linux? http://gutenprint.sourceforge.net/
`- GPG Public Key: 0x25BFB848 Please GPG sign your mail.
Reply to: