RAD Studio 10.4.1 is now available! Learn more. 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


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Learn More About Upgrading

Deep Dive Webinar: Boost C++Builder Compile Speed with TwineCompile
Related posts
News

Using C++17 Algorithms Library Parallel Sorting with C++Builder 10.4 Sydney for Win32 and Win64

C++News

C++Builder and Platforms Support

C++

What's New in the GetIt Package Manager - June 2020

C++

RAD Studio 10.4 Now Available, Learn More

Leave a Reply

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

IN THE ARTICLES