Ex-convict gets 40 years for attempted robbery