How to catch the exception on SFTP?

I just want to send files to remote machine via SFTP, my question is: how can I catch the exception when SFTP command error occur? for example: add a log when network interrupt on SFTP put(or failure on rename the remote file etc.).

the shell run on linux.

Can anybody answer this question?

What happens if you simply try redirecting the stderr of the sftp command to a log file? a la:

sftp <servername> 2>sftp.err

Then sftp.err file should contain all the errors.

HTH