From what I've seen, errors like division by zero and segmentation violations result in signals being forced on a process by the kernel. You can install signal handlers in your C++ program like so:
Code:
#include <exception>
#include <iostream>
#include <signal.h>
using namespace std;
void sighandler(int);
void sighandler(int signum)
{
switch(signum)
{
case 8: cout << "Caught SIGFPE\n"; exit(1);
case 11: cout << "Caught SIGSEGV\n"; exit(2);
default: cout << "I shouldn't be here!\n";
}
}
int main(void)
{
int a = 1, b = 0;
int* d = 0;
if (signal(SIGFPE, &sighandler) == SIG_ERR)
cout << "Couldn't install FPE signal handler!\nExpect a core dump\n";
if (signal(SIGSEGV, &sighandler) == SIG_ERR)
cout << "Couldn't install SEGV signal handler!\nExpect a core dump\n";
#ifndef SKIP
cout << "Div by zero\n";
int c = a/b;
#else
cout << "Playing with bad memory\n";
d[0] = 0;
#endif
}
Once you've run and compiled, you should get
Code:
[mascdman@odin tmp][ :) ]$ ./a.out
Div by zero
Caught SIGFPE
[mascdman@odin tmp][ :( 1 ]$
or
Code:
[mascdman@odin tmp][ :) ]$ ./a.out
Playing with bad memory
Caught SIGSEGV
[mascdman@odin tmp][ :( 2 ]$
To get a list of other signals, run
man -S 7 signal. Hope this helps.
--mascdman