0

In C++, is it possible to programmably access the container that cause std::out_of_range to be thrown in the catch block?

EDIT: I do not intend to debug or recover the problematic container. I simply have two different containers with lambda functions coming in. The flow in the catch block depends on which container is the problematic one.

xuhdev
  • 8,018
  • 2
  • 41
  • 69
  • No. That is completely against the point of exceptions – Passer By Apr 24 '18 at 21:41
  • You might create your own `at` function which throws custom exception with the information you want. – Jarod42 Apr 24 '18 at 21:41
  • No. There are debuggers that will bring you to the location that called `throw` and you can get the callstack, or [use techniques like this](https://stackoverflow.com/questions/2443135/how-do-i-find-where-an-exception-was-thrown-in-c) but just from the `std::exception` you cannot inspect anything about it to find the thrower – Cory Kramer Apr 24 '18 at 21:42
  • 2
    There's no such information in the exception, and the array might well not exist at the point the exception was caught. –  Apr 24 '18 at 21:42
  • @PasserBy Why is that? – xuhdev Apr 25 '18 at 02:31
  • If you were to throw, something irreversibly bad happened at the site and can't be recovered from locally, this is the whole point of exceptions. The object that threw the exception is conceptually broken and in an inconsistent state, accessing it is a flawed design. Exceptions aren't debugging tools, see https://isocpp.org/wiki/faq/exceptions#why-not-exceptions – Passer By Apr 25 '18 at 02:47
  • your query doesn't make sense if you are using the standard library. And what do you mean by containers? Your containers? The container classes of the standard library or some other library? – pcodex Apr 25 '18 at 11:47
  • @PasserBy See my update. – xuhdev Apr 25 '18 at 17:09

1 Answers1

0

If you control the throwing of exceptions then you could try creating your own custom exception and then throwing it as well. Although why would you throw it if you already knew about the exception causing condition. It usually doesn't make sense to throw and catch your own exceptions but in your special case you could attempt to do just that. So you would need to write your own exception class which encapsulates info about the container in question, throw it and then handle it.

However the container's state is questionable at the time of handling the exception.

Think about the following:

  • Does it make sense to throw and catch your own exception?
  • If you do not throw the exception then how would you get info about the container?
  • Why would you even want to access the container?
  • Is the container on the stack or the heap?
  • Who controls the lifetime of the container?
  • Are you rethrowing the exception?
pcodex
  • 1,812
  • 15
  • 16
  • "Does it make sense to throw and catch your own exception?" - why not? It's quite common to derive your own exception types from the standard exceptions and throw and catch them. –  Apr 24 '18 at 22:07
  • yes that was a question not rhetoric. I don't think I said it didn't make sense – pcodex Apr 25 '18 at 11:42