Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can SQL Try-Catch blocks handle thrown CLR errors?

We are using SQL 2005 and the try-catch functionality to handle all of our error handling within the DB. We are currently working on deploying a .NET CLR function to make some WCF calls in the DB. This WCF procedure is written in the CLR and then deployed to SQL. If I put a try-catch block in the CLR code, it catches the error fine. However, I can't seem to throw the error up to the try-catch block in SQL. SQL seems to ignore what I throw it and catches the error it finds. Is there no relation between the two (i.e. I can't throw from one to another?)

If I can throw from within the CLR to the calling procedure in SQL, is any special formatting needed? I tried a specific case of catching the error that was thrown, and then throwing a different error, but SQL ignored my thrown error and caught the original error, as if it ignored the thrown error.

like image 707
Noah Avatar asked Apr 01 '26 06:04

Noah


1 Answers

Here is a blog post that covers it at a highish level: Exception handling in SQLCLR

When SQL server execute a user function/procedure/trigger implemented in CLR (i.e., managed code), we will install a managed exception handler around the user code. So if the user code leaked a exception, the server will catch it and throw a TSQL exception wrapping the user exception.

This seems to imply that it will just work.

like image 106
Larry Smithmier Avatar answered Apr 04 '26 02:04

Larry Smithmier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!