Link to home
Start Free TrialLog in
Avatar of lucand
lucand

asked on

Catching system exceptions using C++ exception handling

Is that posible to catch system exceptions like "access violation" by using C++ exception handling? In that case what will be the thrown object that I can use into the catch() statement? I don't want to use the catch(...) because I need more information about the exception.

Thanks
Avatar of jhance
jhance

It depends.  What operating system are you using?
Avatar of jkr
>>It depends.

Actually, there are just 2 ways - SEH (Win32) or signals (all POSIX).

So, in code

(SEH)
__try {

 // code here

} __except ( EXCEPTION_ACCESS_VIOLATION == GetExceptionCode()) {

throw new XcpMyAccVio ();
}

(POSIX)
void my_sigsegv_handler ( int) {

throw new XcpMyAccVio ();
}

signal ( SIGSEGV, my_sigsegv_handler);
Avatar of lucand

ASKER

I use WindowsNT,2000

I allready tryed signals and _set_se_translator() but not working into a multithreading program(any sugestions?).

SEH is not specifically designed for C++ and I get the following compilation error: "Cannot use __try in functions that require object unwinding"

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lucand

ASKER

SetUnhandledExceptionFilter() is working with multithreading.

Thanks jkr!
Hmmm, like I said originally, "It depends...."

The accepted solution is NOT C++ but rather a Windows-ism.
Well, you could use the 'signal()' approach on Win32 also...
Avatar of lucand

ASKER

Jhance you are right, that is not a pure C++ solution but it's working fine in Windows.

signal() is not working if the exception is into a child thread or if signal() is called from a DLL

Thanks