Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

SEH vs C++ exceptions

4.85/5 (4 votes)
6 Feb 2014CPOL1 min read 13.2K   140  
Intercepting SEH exceptions in C++ program
Download source code

Introduction

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.

Concepts

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.

Background

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 :

C++
//
// 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;
		break;
	case 1 :
		cout << "Access NULL offset ..." << endl;
		{
			int *a = NULL;
			*a = 1;
		};
		break;
	case 2 :
		cout << "Division by zero int ..." << endl;
		{
			int a = 1, i = 0;
			a /= i;
		};
		break;
	case 3 :
		cout << "Division by zero float ..." << endl;
		{
			_asm {	// using fpu execptions
				PUSH WORD PTR 0x0380
				FLDCW [ESP]
				POP	AX
			};
			float a = 1, i = 0;
			a /= i;
		};
		break;
	};
 }
 catch (exception &e) {
	cout << ">>>> Exception : " << e.what() << endl;
 };
 return 0;
}
</iostream></stdexcept></conio.h></float.h></time.h></tchar.h>

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.

License

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