Click here to Skip to main content
Click here to Skip to main content
Go to top

SEH vs C++ exceptions

, 6 Feb 2014
Rate this:
Please Sign up or sign in to vote.
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 :

//
// 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)

Share

About the Author

Adamanteus
Software Developer LLC "AMS"
Russian Federation Russian Federation
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 6 Feb 2014
Article Copyright 2014 by Adamanteus
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid