Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TRY - CATCH with multiple errors


TRY - CATCH with multiple errors

Author
Message
Dave Mason
Dave Mason
SSC-Addicted
SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)

Group: General Forum Members
Points: 430 Visits: 872
If I open a new window in SSMS, attempt to backup a database, and the operation fails, I get two error messages. Here's a simple example that attempts to backup a database snapshot:

BACKUP DATABASE MyDbSnapshot
TO DISK = 'MyDbSnapshot.bak'



Msg 3002, Level 16, State 1, Line 1
Cannot BACKUP or RESTORE a database snapshot.
Msg 3013, Level 16, State 1, Line 1
BACKUP DATABASE is terminating abnormally.


If I try the same db backup in a TRY-CATCH block, I lose the first (and more pertinent) error message:

BEGIN TRY
BACKUP DATABASE MyDbSnapshot
TO DISK = 'MyDbSnapshot.bak'
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
END CATCH



Msg 3013, Level 16, State 1, Line 1
BACKUP DATABASE is terminating abnormally.


How do you access all of the errors in the CATCH block?

Dave Mason
Seminole County, FL

Roy Ernest
Roy Ernest
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2496 Visits: 6853
The try catch will catch only the last message since usually the first message gives the information and the last one gives the final result (Terminating message).
One thing you could try is to call this from a power shell and pipe the output to a file or something.
That is the only way I can think of.

-Roy
GSquared
GSquared
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14385 Visits: 9729
As per its documentation, Try Catch only gets the error that passes control to the Catch block. The only way to do what you want is to do it outside of a Try Catch block.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Grant Fritchey
Grant Fritchey
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17635 Visits: 32268
Also CATCH won't catch syntactical (is that a word) errors, so you may see two errors in fact, but you'll only catch one

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Dave Mason
Dave Mason
SSC-Addicted
SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)

Group: General Forum Members
Points: 430 Visits: 872
Dave Mason (10/8/2010)
Msg 3002, Level 16, State 1, Line 1
Cannot BACKUP or RESTORE a database snapshot.
Msg 3013, Level 16, State 1, Line 1
BACKUP DATABASE is terminating abnormally.



Does SQL Server record the above error messages anywhere? (System table? Other?)
It does not appear to be in the error log. Rather, I see the following, which (again) is not pertinent:
BACKUP failed to complete the command BACKUP DATABASE MyDbSnapshot. Check the backup application log for detailed messages.

Dave Mason
Seminole County, FL

Derrick Smith
Derrick Smith
Mr or Mrs. 500
Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)Mr or Mrs. 500 (594 reputation)

Group: General Forum Members
Points: 594 Visits: 715
In the step of the job on the Advanced tab, specify an output file (.txt). It will log all errors to that.

You should also see more detail in the step of the job history, but that might be truncated. The output file does not get truncated.
Dave Mason
Dave Mason
SSC-Addicted
SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)

Group: General Forum Members
Points: 430 Visits: 872
Derrick Smith (10/12/2010)
In the step of the job on the Advanced tab, specify an output file (.txt). It will log all errors to that.


Thanks, Derrick. That helps some.

Ideally, what I want to do is have my SQL Job create backups for multiple db's. If a backup for a single db fails, I want to capture all the error messages (including the first error message that actually tells me why it failed), and then proceed to the next db backup.

Oh, and I would prefer to do everything in tsql. (Just a personal preference.) I don't have any SSIS packages and I don't use the built-in maintenance plans--I'd like to keep it that way. (Scathing replies may commence now...)

Dave Mason
Seminole County, FL

Dave Mason
Dave Mason
SSC-Addicted
SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)

Group: General Forum Members
Points: 430 Visits: 872
BTW,
I found this related submission on MS Connect: Incorrect error number passed to catch block

Please go there and vote for it as an important issue. (You'll have to login with an MS Live account.)

Maybe MS will address it in an upcoming release or service pack...

Dave Mason
Seminole County, FL

Dave Mason
Dave Mason
SSC-Addicted
SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)SSC-Addicted (430 reputation)

Group: General Forum Members
Points: 430 Visits: 872
Here's a pure t-sql solution to my problem:PJ on Development: Retrieve Error Message

Dave Mason
Seminole County, FL

manub22
manub22
SSC-Enthusiastic
SSC-Enthusiastic (137 reputation)SSC-Enthusiastic (137 reputation)SSC-Enthusiastic (137 reputation)SSC-Enthusiastic (137 reputation)SSC-Enthusiastic (137 reputation)SSC-Enthusiastic (137 reputation)SSC-Enthusiastic (137 reputation)SSC-Enthusiastic (137 reputation)

Group: General Forum Members
Points: 137 Visits: 1861
As this is very old thread.

But just updating this with the solution that the new keyword THROW introduced in SQL Server 2012 will serve the purpose.
THROW in CATCH block will return multiple errors encountered in TRY block.

Check my blog post here: http://sqlwithmanoj.com/2013/01/24/another-reason-to-use-throw-clause-instead-of-raiserror-sql-server-2012/


~manoj | LinkedIn | Facebook | YouTube
Blog: SQLwithManoj.com

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search