Solved

Catching system exceptions using C++ exception handling

Posted on 2002-06-26
8
368 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 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
 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

623 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