# Programmatically assign output Hold[…] produced by errors to a variable

expr = Return[1, 2]


(*Hold[Return[1, 2]]*)


However, Hold[Return[1, 2]] is not assigned to expr.Why does this happen? I want to use some code to store the output into that variable, is it possible?

PS: Please don't manually copy/paste or use methods like read the output cells/notebook contents and its variation. I prefer dealing with the problem directly.

• Have you seen this? – J. M. will be back soon Jul 7 '16 at 4:16
• @J.M. Thanks, that does captures the Expression in error message. But I still wonder why the output is not assigned to the variable. – happy fish Jul 7 '16 at 4:22

Expressions that simply evaluate with messages are not returned wrapped in Hold, and there is no problem assigning these to a Symbol:

expr = Map[1, 2, 3, 4];

?expr


Map::nonopt: Options expected (instead of 4) beyond position 3 in Map[1,2,3,4]. An option must be a rule or a list of rules. >>

Globalexpr

expr = Map[1, 2, 3, 4]


Your use of Return however breaks the normal evaluation order much as Throw or Break would, so Set is never completed. This is really no different from wondering why Print does not print "foo" or "bar" in:

Print["foo", "bar", Return[1, 2]]


Hold[Return[1, 2]]

• Thanks! My original case was that, I was swapping a head to Return in an expression like head@@{1,2} and I wanted to extract information in list {1,2}`, but since the assignment failed, I have to change to other methods. – happy fish Jul 7 '16 at 9:37