Can a literal assignment to int in C++ throw an exception?

c++, exception, try-catch

Solution

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.

Problem

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.]

Original source