Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Try-Catch in Sql 2005 is your friend.

I know that SQL 2005 has been out for a while now, but I did not do ANY sql for over a year.  Anyway, anyone that has had to write anything more than trival procs knows that error handling can be a pain.

In the past you have had to do something along the line of the following

IF ( @@ERROR > 0 )
   -- Do Something

A better way is to wrap your code in Try-Catch and with SQL 2005 you can do this pretty easily.

BEGIN TRY

	-- .... logic goes here

END TRY
BEGIN CATCH
	-- handle here, or simply return
	-- here I wanted to output the error, also showing what can be done
	SELECT
		ERROR_NUMBER() AS ErrorNumber,
		ERROR_SEVERITY() AS ErrorSeverity,
		ERROR_STATE() AS ErrorState,
		ERROR_PROCEDURE() AS ErrorProcedure,
		ERROR_LINE() AS ErrorLine,
		ERROR_MESSAGE() AS ErrorMessage;
END CATCH

One really nice advantage to using the Try-Catch is that errors thrown in subsequent proc calls can be caught and handled.  No more needing to pass back return codes from procs.

Check out the documentation for more information - found here

Till next time,



Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# January 18, 2008 10:02 AM

» Daily Bits - January 19, 2008 (Early Edition) Alvin Ashcraft’s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats. said:

Pingback from  » Daily Bits - January 19, 2008 (Early Edition) Alvin Ashcraft’s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.

# January 18, 2008 9:15 PM

Javier-Romero said:

links for 2008-01-22

# January 22, 2008 4:27 AM

No prescription phentermine. said:

No prescription phentermine. Phentermine 37.5 mg online prescription. Lowest price phentermine no prescription.

# August 5, 2008 2:46 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News