Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
thebrenda
Starting Member
22 Posts |
Posted - 2013-09-04 : 09:22:10
|
I have a TRY CATCH. What is the best way to know that there was not an error. The way our messaging works, i have to write out a second message that says either "Failed" or "Success". The "Failed" will be in the CATCH. But after the CATCH I need to know that there was not an error and the CATCH was not executed. Obviously I can set a variable, just wondering if there is a more streamline approach. |
|
djj55
Constraint Violating Yak Guru
352 Posts |
Posted - 2013-09-04 : 09:24:55
|
There is always the function RAISERROR djj |
|
|
thebrenda
Starting Member
22 Posts |
Posted - 2013-09-04 : 10:09:31
|
How would I use RAISEERROR (now THROW) to know that an error did not occur? |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-09-04 : 10:22:46
|
RAISERROR or THROW is used when you do want to report an error. I wouldn't use it for indicating a no-error condition. If you need a "Success" message, write that as the last message in the TRY block. |
|
|
thebrenda
Starting Member
22 Posts |
Posted - 2013-09-04 : 10:27:57
|
Thank you for the obvious answer. Just put the 'Sucess' as the last line in the TRY block. How do you mark a question answered? |
|
|
denis_the_thief
Aged Yak Warrior
596 Posts |
Posted - 2013-09-04 : 10:36:10
|
I think the best practice is that at the end of your Catch, you return the errorcode: Return ERROR_NUMBER(). What you are saying is you want to continue execution code after you encounter a Catch, I don't think that is a good idea.And end every stored procedure with: Return 0. That way if you call an sp, you'll know by the return value if it errored out or not.But to answer your question, I think the only way to know after the Catch is to set a variable as you mentioned. |
|
|
ScottPletcher
Aged Yak Warrior
550 Posts |
Posted - 2013-09-04 : 12:12:20
|
BEGIN TRY......PRINT 'Success'END TRYBEGIN CATCH......PRINT 'Failed'END CATCH |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-09-04 : 12:19:06
|
quote: Originally posted by James K RAISERROR or THROW is used when you do want to report an error. I wouldn't use it for indicating a no-error condition. If you need a "Success" message, write that as the last message in the TRY block.
I don't disagree that it shouldn’t be used, but people do use RAISERROR with a severity of 11 or lower to pass messages back to the caller. As severity 11 or lower will not cause execution to transfer to the catch bock, thus providing a way to bubble up an informational/warning message.So, the OP, could use that method to get a message back to the calling environment. But, in all reality, we have much richer error handling then we had in the past, so there is no need to return messages and check return codes anymore. |
|
|
|
|
|
|
|