Ask a questionAsk a question
 

AnswerQuestion about catching/notifying error.

  • Tuesday, November 03, 2009 2:52 AMpinoyz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Tuesday, November 03, 2009 4:41 AMGanesh Ranganathan - Bangalore, India Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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
  • Tuesday, November 03, 2009 5:31 AMGanesh Ranganathan - Bangalore, India Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
  • Tuesday, November 03, 2009 9:44 PMMalange Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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!

All Replies

  • Tuesday, November 03, 2009 3:52 AMRenee CulverMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Get rid of the 'is'. :)
    Renee
  • Tuesday, November 03, 2009 4:20 AMpinoyz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Tuesday, November 03, 2009 4:41 AMGanesh Ranganathan - Bangalore, India Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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
  • Tuesday, November 03, 2009 5:25 AMpinoyz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Tuesday, November 03, 2009 5:31 AMGanesh Ranganathan - Bangalore, India Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
  • Tuesday, November 03, 2009 5:44 AMpinoyz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks for the advice Ganesh.

    I think I'll stick to the cathing the error/message in a catch block.


    noob vb programmer
  • Tuesday, November 03, 2009 9:44 PMMalange Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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!