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.

 All Forums
 Old Forums
 CLOSED - General SQL Server
 TRY CATCH block.

Author  Topic 

Hariarul
Posting Yak Master

160 Posts

Posted - 2006-09-18 : 02:02:42
Hi,

Is it a good practice to use TRY - CATCH block in all the stored procedures even it does a simple query / manipulation ?

Thanks,

HHA


harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-09-18 : 02:07:16
It's always better to be prepared for the worst...Even though it is a simple query, things may go wrong. In such circumstances, handling errors gracefully is always good and make your application more robust and reliable.

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-09-18 : 03:18:32
http://www.sqlservercentral.com/columnists/rVasant/exceptionhandlinginsqlserver2005.asp

Chirag
Go to Top of Page

Hariarul
Posting Yak Master

160 Posts

Posted - 2006-09-19 : 06:25:53
Thanks !!
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-09-19 : 08:15:15
What would you do with the error?

I tend to use it in control SPs.
If you are just calling an SP and are just going to re-raise any error then there doesn't seem to be much point.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Hariarul
Posting Yak Master

160 Posts

Posted - 2006-09-22 : 05:58:39
Throw it to the application layer
Go to Top of Page

mr_mist
Grunnio

1870 Posts

Posted - 2006-09-22 : 08:43:14
quote:
Originally posted by Hariarul

Throw it to the application layer



Well SQL Server would do that anyway, so there's not much point unless you are either going to alter the error message somehow, or otherwise do some activity that would not take place were it not for the CATCH block.

-------
Moo. :)
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-09-22 : 09:43:01
Really? Are you guys saying that you wouldn't handle all errors and just let SQL Server raise out?

Is that right?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

mr_mist
Grunnio

1870 Posts

Posted - 2006-09-22 : 10:14:01
quote:
Originally posted by X002548

Really? Are you guys saying that you wouldn't handle all errors and just let SQL Server raise out?




That's not what I said. Can't speak for the other people.

I said that if all the OP was going to do in the CATCH block is re-raise the original error, there's no point in the CATCH block.

Personally, I'd write some code in the CATCH block that either fixed the issue, or did some other kind of error logging / reporting.

-------
Moo. :)
Go to Top of Page
   

- Advertisement -