Problem:
When you call a third-party library in your project, use the Try{}catche (...) {} statement, exception cannot be caught.
Solution:
Enabling C + + Exceptions in Project properties->c/c++-> code generation is the third option: Yes, but there is an SEH exception (/EHa) that can catch an exception. (VS2005 SP1)
Related knowledge:
1 C + + and Windows exception handling (Try,catch; __try,__finally; __try, __except)
Original address: http://shijuanfeng.blogbus.com/logs/178616871.html
2 error handling and exception handling, which one do you use
Original address: http://bbs.chinaunix.net/thread-142587-1-1.html
Usage examples:
1 Original address: http://www.doyj.com/2006/09/11/try-catch/
Write data to illegal addresses, use try catch to not catch exceptions in release mode, use __try{}__except () {} to capture
1#include"stdafx.h"2#include <vector>3#include <iostream>4#include <cstdlib>5#include <afxwin.h>6 using namespacestd;7 int_tmain (intARGC, _tchar*argdv[])8 {9 __tryTen { Onebyte*pch; APCH = (byte*)00001234; -*PCH =6; - } the __except (Exception_execute_handler) - { -printf"%s","Access violation"); - } + -System"Pause"); + return 0; A}View Code
2 Original address: Http://zhidao.baidu.com/link?url= Hnbpfn6ekae-8l43ejewqb1crmzjyqxjrkzhg66ngzkdrplhhsbdwwz3um1raqiw34cbytemyqdvendazxldso0suzcuxwiyyhlkc5vojqo
Divisor cannot be a zero example
1#include"stdafx.h"2#include <vector>3#include <iostream>4#include <cstdlib>5 using namespacestd;6 7 voidDivision (intDividend,intdivisor)8 {9 intresult =0;Ten if(Divisor = =0)Throw("the divisor cannot be zero"); Oneresult = dividend/Divisor; Acout<<result<<Endl; - } - int_tmain (intARGC, _tchar*argdv[]) the { - Try - { -Division (4,0); + return 0; - } + Catch(Const Char*err_msg) A { atcout<<err_msg<<Endl; - } - - -System"Pause"); - return 0; in}View Code
Try{}catch (...) {}