Solved

Catching system exceptions using C++ exception handling

Posted on 2002-06-26
8
363 Views
Last Modified: 2008-03-17
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
0
Comment
Question by:lucand
  • 3
  • 3
  • 2
8 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 7112338
It depends.  What operating system are you using?
0
 
LVL 86

Expert Comment

by:jkr
ID: 7112417
>>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);
0
 

Author Comment

by:lucand
ID: 7113972
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"

0
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 7114071
>>SEH is not specifically designed for C++ and I get the
>>following compilation error: "Cannot use __try in
>>functions that require object unwinding"

Yes, but this is not a problem. You would overcome this by simply not using the SEH frame handler in functions that have non-scalar data types.

Even easier, you could use 'SetUnhandledExceptionFilter()', e.g.

LONG
UnhandledExceptionFilter(
  STRUCT _EXCEPTION_POINTERS *ExceptionInfo   // address of
                                              // exception info
)
{
 if ( EXCEPTION_ACCESS_VIOLATION == GetExceptionCode() {

  throw new XcpMyAccVio ();
 }

 return EXCEPTION_EXECUTE_HANDLER;
}
 

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:lucand
ID: 7114383
SetUnhandledExceptionFilter() is working with multithreading.

Thanks jkr!
0
 
LVL 32

Expert Comment

by:jhance
ID: 7114396
Hmmm, like I said originally, "It depends...."

The accepted solution is NOT C++ but rather a Windows-ism.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7114441
Well, you could use the 'signal()' approach on Win32 also...
0
 

Author Comment

by:lucand
ID: 7114511
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now