Click here to Skip to main content
13,290,176 members (57,220 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 19 Apr 2010

Using Exception Handling in VC++

, 19 Apr 2010
Rate this:
Please Sign up or sign in to vote.
It will help to understand the usefulness of writing codes with good programming practice


Like every Windows user, you probably have suffered more than one time from this annoying occurrence: You work on an application, and then suddenly, without any reasonable explanation, the application crashes, and system message box appears on the screen, telling you that the application is going to be terminated. This crash window varies from one operating system to another: In Windows 98, for example, the following text is displayed: 'This program has performed an illegal operation and will be shut down'. Windows 2000 and Windows NT, as opposed to previous versions of Windows, display the details of the problem that caused the exception. For example: 'The instruction at "0x00401000" referenced memory at "0x00000000" The memory could not be written.'
However, there is one common thing in all Windows operating system: In such an event, you lose all your work you have done since the last save operation.


Around a years back, while writing a huge app, (consisting around 93 internal projects), where there were around 15 threads running constantly and communicating with each other, I'd faced several problems. Most of the time in the middle of the program, it crashed.

This problem mostly occurred due to a bug in the application itself, or in one of the components or libraries of the operating system. Luckily, the programmer has the ability to avoid this kind of annoying crashes, by applying a simple exception handling mechanism.

Borland Delphi development tool is a good example of effective crash handling: The executables created by this tool has a special built-in exception handling routine, so whenever an exception occurs in a Delphi program, a special dialog-box of Delphi is displayed. This dialog-box contains some information about the problem that caused the exception. After the user clicks 'Ok', the program continues to run properly.

Visual C++ (as well as other development tools from Microsoft) doesn't provide an automatic exception handling module like Delphi, so if you want to avoid crashes in your C/C++ application, you have to explicitly add exception handling routines to your software.

The ExceptionTest

The ExceptionTest example demonstrates how to avoid application crash, by using the __try and __except statements. When you run the sample, you get a dialog-box containing 2 buttons. The first button raises the exception without exception handling block, and this causes the application to crash immediately. The second button raises the exception from within a __try block. When the exception occurs, the function inside the __except statement (GetExceptionInformation) is called. This function displays a special dialog-box with a little information about the exception, and allows the user to decide whether he wants to continue to run the program or to terminate it. The user can also copy the exception information to the clipboard.

void HandledExceptionTest() 
  //This function causes the exception
 //If an exception occurs, display our exception dialog:
void UnhandledExceptionTest() {
 //This function causes the exception. 
 //Because there is no exception handling here, the program will crash.


  • 19th April, 2010: Initial post 


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


About the Author

Software Developer (Senior)
India India
My main area of experience has been in VC++/ MFC / COM projects where I had to write interfacing program for the third Party hardware devices, real time Robot and port communication program and Socket Programming using RF communication. To design & develop Software Applications for different types of Mechatronic Devices like SONAR, SICK LMS LASER, Under Water Analog Camera, High Performence Motor etc. I have also worked in web-based development projects and database management system developments for various clients.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
dotnethelps18-Dec-10 4:07
memberdotnethelps18-Dec-10 4:07 
GeneralMy vote of 1 Pin
PogoboyMtK29-Apr-10 4:36
memberPogoboyMtK29-Apr-10 4:36 
GeneralMy vote of 1 Pin
aphazel 死神21-Apr-10 4:04
member aphazel 死神21-Apr-10 4:04 
GeneralMy vote of 1 Pin
stebich21-Apr-10 0:35
memberstebich21-Apr-10 0:35 
GeneralMy vote of 1 Pin
emilio_grv20-Apr-10 21:06
memberemilio_grv20-Apr-10 21:06 
GeneralMy vote of 1 Pin
basementman20-Apr-10 4:51
memberbasementman20-Apr-10 4:51 
GeneralMy vote of 1 Pin
dotnethero220-Apr-10 0:24
memberdotnethero220-Apr-10 0:24 
GeneralCan't download Pin
petr319-Apr-10 20:03
memberpetr319-Apr-10 20:03 
GeneralRe: Can't download Pin
stebich21-Apr-10 0:42
memberstebich21-Apr-10 0:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 19 Apr 2010
Article Copyright 2010 by @Intersect☺™
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid