6

Given code like the following:

void f()
{
   int i;
   i = 0;
}

is it possible the system could throw an exception due to the simple assignment?

[Edit: For Those saying, "No an exception cannot occur," can You point Me in the direction of the part of the C++ standard which says this? I am having trouble finding it.]

2
  • 6
    is the type always int and the RHS always a literal? Or are you asking about the general case? (Also what you showed wasn't an assignment)
    – Flexo
    May 16, 2012 at 16:04
  • Thanks for catching that point. I edited the example. To Your first question, for both, actually.
    – xuinkrbin.
    May 16, 2012 at 16:06

5 Answers 5

8

Although you'd probably be hard put to find an assurance of it in the standard, a simple rule of thumb is that anything that's legitimate in C probably can't throw. [Edit: The closest I'm aware of to a direct statement to this effect is at §15/2, which says that:

Code that executes a throw-expression is said to “throw an exception;” [...]

Looking at that in reverse, code that does not execute a throw-expression does not throw an exception.]

Throwing is basically restricted to two possibilities: the first is invoking UB. The second is doing something unique to C++, such as assigning to a user-defined type which overloads operator =, or using a new expression.

Edit: As far as an assignment goes, there are quite a few ways it can throw. Obviously, throwing in the assignment operator itself would do it, but there are a fair number of others. Just for example, if the source type doesn't match the target type, you might get a conversion via a cast operator in the source or a constructor in the target -- either of which might throw.

4
  • Implementations are free to impose limits on resources aren't they? If they are wouldn't "no more stack and no registers spare" (at least theoretically) be a valid reason to throw and only spotted at first use?
    – Flexo
    May 16, 2012 at 16:16
  • In theory yes. VC++6 (for one example) did things like that -- but it worked out poorly enough that VC++ doesn't any more, and I'd be at least a little surprised to see anybody else try it anytime soon either. May 16, 2012 at 16:19
  • @JerryCoffin For a sufficiently strict definition of "legitimate in C". Assigning a floating point to an int can result in undefined behavior, and an exception is just as valid as anything else in the case of undefined behavior (although perhaps not from a quality of implementation point of view). May 16, 2012 at 16:37
  • 2
    @awoodland I can never find it, but in the C standard (and I'm pretty sure that C++ follows C here), running out of resources (stack, etc.) is undefined behavior. Which means that anything goes, although from a quality of implementation point of view... May 16, 2012 at 16:40
7

There's quite a lot of things that look like assignments that can throw one way or another:

int operator"" _t(const char *) { throw 0; } // C++11 user defined literal 

struct foo {
  foo(int) { throw 0; }
  operator int() { throw 0; }
  foo& operator=(int) { throw 0; }
};

int main() {
  int i;
  i = 0; // can't throw
  i = 0_t; // User defined literal throws
  foo f = 0; // Constructor throws
  i = f; // conversion operator throws
  f = 0; // assignment throws
  f = f; // both conversion and assignment would like to throw
}

(including the new one from C++11)

4

If you're concerned about assinging 0 (which has type int) to an int, §5.17 of the standard specifies very exactly the semantics of the assignment operation, and there is no case where an exception can occur. If you're concerned about assigning an arbitrary expression to an int, §5.17 says that “the expression is implicitly converted to the cv-unqualified type of the left operand." Depending on the actual type of the right operand:

  • If it is an integral type, if the actual value cannot be represented in an int, the results are implementation defined (the C standard is more explicit: it must either result in an int with an implementation defined value, or you will get an implementation defined signal)

  • If it is a floating point value, the result is undefined behavior if the value after truncation to zero cannot be represented in an int, the behavior is undefined (so you might get an exception)

  • If it is a user defined type, then a user defined conversion operator will be called. Which can throw an exception.

If you're concerned about assigning other types: for each set of non-class types, there is a list of rules like the above, but the only possible exceptions will be as a result of a type conversion. For class types, the operator= will be used. Which can throw an exception, depending on what is in it.

2

It can throw an exception only in one case: when overloaded operator=() for those two types throws; similary, when conversion is needed, the converting constructor or operator T() can also throw. It depends on the exact implementation then - to find out if it will throw, look for information about it in documentation of library you are using.

0

if it's just an int, then no - it will not throw.

if it's something more complex, such as a vector, then it may throw for a number of reasons (e.g. allocation failure or alteration from a secondary thread).

1
  • Allocation failure is defined to throw an exception (unless you're using a custom allocator). Accessing a vector from another thread while you're modifying it is undefined behavior, and anything can happen. From a quality of implementation point of view, however, I would not expect an exception. May 16, 2012 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.