Click here to Skip to main content
15,945,334 members
Articles / Desktop Programming / Win32

SEH vs C++ exceptions

Rate me:
Please Sign up or sign in to vote.
4.85/5 (4 votes)
6 Feb 2014CPOL1 min read 13.1K   139   9  
Intercepting SEH exceptions in C++ program
Download source code


As from practical point of view, not intercepted in program system exceptions, worse general user imagination of it, present code allowing to re-throw system exceptions to standard C++ exceptions.


Problem of throwing programming exceptions instead of system exceptions, is solved by vector exception handlers entered from Windows 5.0. Those handlers are called before structural exceptions handlers, and in it possible to re-throw exceptions to programming system, which is easy to catch in program code — actually having stopped processing original exception.

As result, address of code and state of processor registers at moment of original exception will be lost, but in majority of cases given information is not finds practical application, and can be replaced by text description of exception type, that is understandable to user.


Most often, system exceptions are : memory access violation and exceptions thrown by processor and mathematical coprocessor as hardware interrupts, when division into zero, overflow or denormalisation of operand and etc.

Catching given types of exceptions allows, not only make algorithms more reliable, but also increasing speed of computing, when having stopped at overflow moment or zero division, actually having intercepted hardware interrupt in C ++ code.

Using the code

For use it is need add module Excpt to program, and set vector exception handlers, as it done in beginning of demonstration program :

// SEHvsCPP.cpp : SEH vs C++ exceptions demo program
#include <tchar.h>
#include <time.h>
#include <float.h>
#include <conio.h>
#include <stdexcept>
#include <iostream>
#include "excpt"

#define	kbhit	_kbhit
#define	getch	_getch

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
 setvect(4, excpt_zerodo_hard_error);
 setvect(6, excpt_wrongop_hard_error);
 setvect(0x24, excpt_dos_hard_error);

 srand((unsigned int)time(NULL));
 while (true)
 try {
	if (kbhit()) { getch(); getch(); };
	switch (rand() % 4)
	case 0 :
		cout << "Normal execution ..." << endl;
	case 1 :
		cout << "Access NULL offset ..." << endl;
			int *a = NULL;
			*a = 1;
	case 2 :
		cout << "Division by zero int ..." << endl;
			int a = 1, i = 0;
			a /= i;
	case 3 :
		cout << "Division by zero float ..." << endl;
			_asm {	// using fpu execptions
				PUSH WORD PTR 0x0380
				POP	AX
			float a = 1, i = 0;
			a /= i;
 catch (exception &e) {
	cout << ">>>> Exception : " << e.what() << endl;
 return 0;

Further in program there can be any exception situations, but it nevertheless and will be reliable to work.

Points of Interest

Apparently from example code first lines, seemed no differ from same lines of reliable program in MS-DOS system, that used vectors of interrupts for processing exceptions situations — in what it is necessary to give due Microsoft Corp., so it in high compatibility of all those systems.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer LLC "AMS"
Russian Federation Russian Federation
In 1991 year has received senior secondary education and entered in Moscow Economic-statistical Institute, by faculty of Supply mathematics, has studied to 2-nd course with high progress, and left it by own will.

With 1994-1996 years - serviced in army (navy fleet, now sailor of a stock).

Since 1999 year has begun my labour career, with job of computer operator, in department of computer maintenance developing learn-methodical documentation.

Single, on religion - atheist.

Comments and Discussions

-- There are no messages in this forum --