Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++

Catching an access violation exception

Author: Embarcadero USA

 Question and Answer Database

FAQ1750C.txt   Catching an access violation exception
Category   :C/C++ Language Issues
Platform    :All
Product    :BC++  5.x

Question:

Why does my attempt at catching an access violation fail?

main()
{
  char *ptr;
  ptr=NULL;
  try
  {
    *ptr=5;
  }
  catch(...)
  {
    cerr << "Caught exception" <"throw" somewhere for you to catch anything ).
You could use that construct to handle thrown objects ( like 
xalloc for failures in calls to new ).
For OS excpetions like access violations, you should be using 
OS structured exception handling:


// catch.cpp

#include 
#include 

main()
{
  char *ptr;

  // point the pointer to an
  // invalid address
  ptr=NULL;

  try
  {
    // attempt to assign a value
    // to the location pointed
    // to by the pointer
    *ptr=5;
  }
  except(EXCEPTION_EXECUTE_HANDLER)
  {
    // report the exception type
    cerr << "Caught exception code: 0x"
      <<<

Article originally contributed by

RAD Studio 13.2 Florence Now Available! Kai 1.1.1 Now Available! What's Coming in RAD Studio 13.2 Florence

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.

Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES