0

This is a basic question after coding try which covers a range of c/c++ statements.

If the catch block is entered, how do I know which statement was the culprit?

RvdK
  • 19,580
  • 4
  • 64
  • 107
  • You don't. If you need that kind of granularity, then either maintain a state variable to identify each step, or split the big `try` into individual `try` blocks around each statement. – dxiv Jul 27 '16 at 02:46
  • If you need to know, chances are that your code isn't exception safe (with either the *basic* or *strong* guarantee). Too little information to give a helpful answer. Please provide a specific code sample and explain, why it matters to you. – IInspectable Aug 03 '16 at 13:13

1 Answers1

0

Given the code

try {
    if (a)
        throw 0;
    else if (b)
        throw 0;
    else if (c)
        throw 0;
    throw 1;
} catch (int i) {
    // << here
}

If i is 0, there is no language-specified mechanism to tell which throw 0 threw. We can only distinguish between 0 and 1 throws. There are compiler specific hacks (How can I print stack trace for caught exceptions in C++ & code injection in C++) but nothing standard.

Community
  • 1
  • 1
kfsone
  • 23,617
  • 2
  • 42
  • 74