SQL Server supports Error handling efficiency. You can do this with the help of TRY and CATCH blocks. If there is an error you can get the exact error message using the system function ERROR_MESSAGE().
I will demonstrate it using the following code
The result you get is
It is because number 1 cannot be added to a string
But ERROR_MESSAGE will work only inside the CATCH block. If you use it outside of it, you will get nothing
If you execute the above, you get the following two result sets
As you see ERROR_MESSAGE() returns NULL when used outside of CATCH block.