织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>杂项技术>VC(MFC)>

通过异常处理机制来避免程序崩溃

罗索客 发布于 2008-04-20 22:09 点击:次 
Preventing application crash by using exception handling mechanism Introduction As every Windows user, you probably have suffered more than one time from this annoying occurrence: You work on application, and then suddenly, without any reasonable ex
TAG:

Preventing application crash by using exception handling mechanism

Introduction
As every Windows user, you probably have suffered more than one time from this annoying occurrence: You work on application, and then suddenly, without any reasonable explanation, the application crashes, and system message box appears on the screen, telling you that the application 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 event, you lose all your work you have done since the last save operation.


新窗口浏览

新窗口浏览

This problem is mostly occurred due to a bug in the application itself, or in the 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 build-in exception handling routines, so whenever an exception is occurred 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 Example
The ExceptionTest project example demonstrate 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 that deliberately causes an exception. 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 is occurred, the function inside the __except statement (GetExceptionInformation) is called. This function displays a special dialog-box with a little information about the exception, and allow 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 in order to send it to the author of the software.


新窗口浏览

Download ExceptionTest Sample (For running in Visual C++ 6.0)

点击浏览该文件 (iwgh)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/200804/6938.html]
本文出处: 作者:iwgh
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容