Catching Acces Violation/Segmentation Fault

BCS ao at pathlink.com
Wed May 9 11:58:25 PDT 2007


Reply to silverling,

> I am programming a Matrix class to be used like a primitive type
> (overloaded operators, identity, transpose, the whole shebang) and one
> of the constructors _may_ cause an Access Violation (or Segmentation
> Fault, if you prefer the old Linux SIGSEG) if the class's user is
> foolish. Is there a way to catch this errors?

Put this in somewhere at global scope and all seg-v's will throw an Error
by passing signal a different function you can make it do other things.

	// stubs for access to unix signal handeling 
alias void function(int) sighandler_t;

extern (C) sighandler_t signal(int signum, sighandler_t handler);

const int SIGHUP	= 1;	// Term Hangup detected on controlling terminal or 
death of controlling process
const int SIGINT	= 2;	// Interrupt from keyboard
const int SIGQUIT	= 3;	// Quit from keyboard
const int SIGILL	= 4;	// Illegal Instruction
const int SIGABRT	= 6;	// Abort signal from abort(3)
const int SIGFPE	= 8;	// Floating point exception
const int SIGKILL	= 9;	// Kill signal
const int SIGSEGV	= 11;	// Invalid memory reference
const int SIGPIPE	= 13;	// Broken pipe: write to pipe with no readers
const int SIGALRM	= 14;	// Timer signal from alarm(2)
const int SIGTERM	= 15;	// Termination signal

static this()
{
		// trap seg-v and report as error (not sure this is working)
	signal(SIGSEGV,function void(int i){throw new Error("SEGV");});
}




More information about the Digitalmars-d-learn mailing list