Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Catching system exceptions using C++ exception handling

Posted on 2002-06-26
8
Medium Priority
?
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
jkr earned 200 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
 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

704 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