Try catch

Hello, I never use try catch for my sp. Is it a good practice to use it? Will it affect on performance?
Does begin trans, commit trans and rollback still works?
Is there any best way to log the errors?

Yes, it's good practice, depending on the situation. Do you want to handle errors yourself and continue if the error isn't "bad" enough? Try/catch is the only way. e.g. we use it in a few places to handle intermittent deadlocks. If the error is a deadlock, we wait a bit and try again, with configurable wait times and retries. Solves that problem for us.

No measurable effect on performance.

BEGIN/COMMIT/ROLLBACK still work

Logging errors? Well, how do you do it now? Do you have a logging table? You could set one up to hold whatever you need.

1 Like

In my opinion, Try/Catch blocks are only appropriate in 'end user code'. By that, I mean the final function before an interface boundary. For example, a button's Click event or a web service function.

Errors should be passed through the call stack as-is, unless you are clarifying the error.

1 Like