- If contract A creates contract B and then throws, is B also deleted?
A throw
will only terminate it's function. It cannot delete it's contract or any child contracts. For example (from the solidity docs):
contract Ballot{
function giveRightToVote(address voter) {
if (msg.sender != chairperson || voters[voter].voted) {
throw;
}
voters[voter].weight = 1;
}
}
If the giveRightToVote()
function is called and if msg.sender != chairperson || voters[voter].voted
, then the giveRightToVote()
function will completely stop running; nothing past the throw will be executed - in this case we see voters[voter].weight = 1
will not be executed.
However! The Ballot{}
contract as a whole is still available on the blockchain for anyone else to call. And in fact giveRightToVote()
could even still be called again, along with any other available functions within the contract.
If Ballot{}
(Contract A) had created any Contract B. A throw in Ballot{}
would not affect any of it's child contracts (Contract B).
- If contract A creates contract B, and B throws in its constructor, does the exception bubble up to A?
A constructor is a function, so the same is true here. If a constructor throws, the rest of the constructor function will not be executed. But again, even after the constructor is thrown, the contract will remain and any other function within contract B will be available for future calls. Contract A will not be affected.