Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch integer divide by zero and access violation exceptions in native C++

I need to catch Integer divide by zero and access violation reading or writing a protected memory and display my own dialog, and do something approprate. These exception can not be catched by try {} catch {} mechanism because hardware generates these exceptions. How can I catch there exceptions? Any suggestions or reference to related articles appreciated mr.abzadeh

like image 655
Reza Abzadeh Avatar asked Oct 07 '11 08:10

Reza Abzadeh


2 Answers

See the documentation for __try / __except mechanism. This is used in Windows for catching hardware exceptions.

like image 83
Jens Avatar answered Nov 16 '22 18:11

Jens


There is a very easy way to catch any kind of exception (division by zero, access violation, etc.) in Visual Studio using try -> catch (...) block :)

A minor project tweaking is enough. Just enable /EHa option in project settings. See Project Properties -> C/C++ -> Code Generation -> Modify the Enable C++ Exceptions to "Yes With SEH Exceptions". That's it!

See details here: http://msdn.microsoft.com/en-us/library/1deeycx5(v=vs.80).aspx

like image 2
Volodymyr Frytskyy Avatar answered Nov 16 '22 18:11

Volodymyr Frytskyy