Get the last message

I am making a script to compile a code for me. But if compile isn’t
sucessfull, I want to put the message erro in a variable and send it to
me.

How can I get the message erro?

Thanks