In this code, or rather in the try {} block, an error occurs.

try { data = (char*)realloc(data, value); } catch (...) { data = (char*)realloc(data, value); } 

I want to put a label in the catch block to catch the error. Why is it not working in Visual Studio ... Do you need some settings?

Many people write that realloc does not give exceptions ... it also gives, here is the simplest example:

 #include "stdafx.h" #include "stdlib.h" int main() { char *a = (char*)malloc(10); free(a); a = (char*)realloc(a, 20); return 0; } 
  • Must catch. Try to explicitly set DebugBreak(); - nick_n_a
  • There can be no C ++ exceptions in this code, so you need to catch system exceptions . They are not caught by a simple catch. - ixSci
  • 2
    try/catch nothing to do with any error trapping, and the realloc function does not throw exceptions. And, by the way, what does the [C] tag do here? - AnT
  • one
    This is not an exception, it is Undefined Behavior, a blunder on the programmer's side. It does not need to catch, it must not be allowed. - VladD
  • 2
    @sitev_ru: None. try/catch never meant to catch unexpected drops in the program. And the exception in C ++ is precisely the situation when the code makes a throw . realloc doesn't know anything about throw , so it can't do it. The motley "departures" in the program have no relation to C ++ exceptions. (Read about SEH if you want, but this is not try/catch and not C ++). - AnT

2 answers 2

Let's start with the fact that realloc does not generate any exceptions.
Let's continue by the fact that even if you address to the zero address, it will not be a C ++ exception, but even an * asynchronous structured exception *. To which try/catch is irrelevant.
But if you really want to - then in VC ++ you can use the /EHa , which allows try/catch to catch these exceptions. Try with him ...

But, frankly, I would not recommend using it - there are more troubles than benefits.

  • Edited the answer, showed that realloc generates exceptions - sitev_ru
  • Are you kidding, right? You have done the purest! classical! undefined behavior - and no more than that! This compiler in this operating system made an emergency stop - is this an exception? You just do not understand the terms ... - Harry
  • OK, this is no exception ... But how to catch it? - sitev_ru
  • Well, I wrote to you - if you want to catch it as an exception (for your responsibility!) - the /EHa (in Visual C ++), and a memory failure (if it happens! By the way, this code has a chance and does not cause a failure, but failure will be in a completely different place ...) will be turned into an exception ... - Harry

Many people write that realloc does not give exceptions ... it also gives, here is the simplest example:

You see a bug in your code, but you decide that this is an exception. What actually happens.

 // тут выделили память, все ок. char *a = (char*)malloc(10); // освободили память, тоже ок. Но free не меняет значение указателя. Он продолжает указывать на тот же кусок памяти. free(a); // а теперь пытаемся обратиться к этой памяти... a = (char*)realloc(a, 20); 

What does realloc actually do? if it is greatly simplified, it allocates a new memory of the required size, copies the old memory to a new place, and frees the old memory. But any programmer knows that if you make free twice for a pointer, you will get an error. Approximate realloc code can be peeped and there are no obvious throws of exceptions.

So what's the "exception"? The debugger and compiler know that the programmer can make a mistake. And in the debug mode, add any checks. And if something is wrong, it intercepts immediately.

That is, you see an exception not from realloc, but from the debug memory manager and debugger, who intercepted the situation of double memory release (or maybe accessing the freed memory, but this is less likely) and scolding you.