Question about catching/notifying error.
- Hi,
Is it ok(best practice) to catch the error or to notify the user from catch statement (try and catch)
for example i want to notify the user that the data is already exists.
So here's my stored proc
If exists (Select Username From UsersTable Where Username = @Username)
begin
raiserror('User is already exists',16,1)
end
noob vb programmer
Answers
You can raise the error in the stored proc or return a status message as an out parameter. Exception handling is more expensive than setting an error code.
I usually raise the error in the stored proc which is caught in the catch block of the data layer, which is then sent to the aexception handler which logs and notify the user.
Ganesh Ranganathan
[Please mark the post as answer if it answers your question]
blog.ganeshzone.net- Marked As Answer byJeff ShanMSFT, ModeratorMonday, November 09, 2009 1:34 AM
- Are you looking for any alternative approach? If so,you could set a status code variable as out parameter which could have an error code and message. In the code you could check for this status code and throw the exception.
Though this will be sligtly better in performance, I feel comfortable in letting my exceptions propogate right from the database to the handler.
Ganesh Ranganathan
[Please mark the post as answer if it answers your question]
blog.ganeshzone.net- Marked As Answer byJeff ShanMSFT, ModeratorMonday, November 09, 2009 1:34 AM
Thanks for the advice Ganesh.
I think I'll stick to the cathing the error/message in a catch block.
noob vb programmer
you can CREAT your code to catch the specific error refered above. If you try to catch the error from error msg genareted by or using:
Catch ex As Exception ....that will not throw the error that you are trying to catch. What gonna happen is will genaret more than 35 lines of code or error; like the ID does exit in database.
if you want we can provide a code that meets your question....
Don't judge me, just Upgrade me. Thanks!- Marked As Answer byJeff ShanMSFT, ModeratorMonday, November 09, 2009 1:34 AM
All Replies
- Get rid of the 'is'. :)
Renee - Hahaha... wrong grammar...
Hi ReneeC.
It's long to be here again... I moved to c# but's my heart still in VB. hehe.
noob vb programmer You can raise the error in the stored proc or return a status message as an out parameter. Exception handling is more expensive than setting an error code.
I usually raise the error in the stored proc which is caught in the catch block of the data layer, which is then sent to the aexception handler which logs and notify the user.
Ganesh Ranganathan
[Please mark the post as answer if it answers your question]
blog.ganeshzone.net- Marked As Answer byJeff ShanMSFT, ModeratorMonday, November 09, 2009 1:34 AM
- Hi Ganesh,
Actually i do the same thing. I catch the error/message from the catch block. which return the 'User already exists' as a message.
noob vb programmer - Are you looking for any alternative approach? If so,you could set a status code variable as out parameter which could have an error code and message. In the code you could check for this status code and throw the exception.
Though this will be sligtly better in performance, I feel comfortable in letting my exceptions propogate right from the database to the handler.
Ganesh Ranganathan
[Please mark the post as answer if it answers your question]
blog.ganeshzone.net- Marked As Answer byJeff ShanMSFT, ModeratorMonday, November 09, 2009 1:34 AM
Thanks for the advice Ganesh.
I think I'll stick to the cathing the error/message in a catch block.
noob vb programmerThanks for the advice Ganesh.
I think I'll stick to the cathing the error/message in a catch block.
noob vb programmer
you can CREAT your code to catch the specific error refered above. If you try to catch the error from error msg genareted by or using:
Catch ex As Exception ....that will not throw the error that you are trying to catch. What gonna happen is will genaret more than 35 lines of code or error; like the ID does exit in database.
if you want we can provide a code that meets your question....
Don't judge me, just Upgrade me. Thanks!- Marked As Answer byJeff ShanMSFT, ModeratorMonday, November 09, 2009 1:34 AM

