Professional Web Applications Themes

error handling in c# - ASP.NET General

If your function throws an exception, you should be prepared to catch it - otherwise it is unhandled and asp.net just displays the error. Put it in a try/catch. On a separate note, what about having your function return true/false depending on if the login was successful? "suzy" <com> wrote in message news:%235xY$phx.gbl...  how ...

  1. #1

    Default Re: error handling in c#

    If your function throws an exception, you should be prepared to catch it -
    otherwise it is unhandled and asp.net just displays the error.

    Put it in a try/catch.

    On a separate note, what about having your function return true/false
    depending on if the login was successful?

    "suzy" <com> wrote in message
    news:%235xY$phx.gbl... 
    how 


    Marina Guest

  2. #2

    Default Re: error handling in c#

    You have to catch the exception in your page:

    try
    {
    UserExists(name, pass);
    }
    catch(UnkownUserExcption e)
    {
    show something to the user
    }

    But you shouldn't use exception handlingfor this, because it is fairly
    expensive. You should only use it for exceptional conditions that normally
    do not happen, e.g. your database can't be reached.

    Greetz,
    -- Rob.

    suzy wrote: 


    Rob Guest

  3. #3

    Default Re: error handling in c#

    You should be able to catch the error in the ASPx page:

    try
    {
    YourLoginComponent.Login();
    }
    catch (exception e)
    {
    // do your "logon failed" stuff here. Your error message is in e.Message
    }

    Now, I'm going to digress:
    You could use the exception here as you have set out to do. However,
    Exception handling is expensive, and generally the advice that you recieve
    in books and NG posts will be to use exceptions for real errors, and not to
    handle expected conditions.

    Example:
    A logon attempt is supposed to either succeed or fail, based on the user's
    credentials. So, you might consider not throwing an exception over a bad
    password. That _might_ just mean that you should return false or
    something. That would be more performant, and a failure to log on due to
    bad credentials is an expected outcome.

    On the other hand, if the logon failed because your logon component was
    unable to connect to the authentication server (active directory, or the SQL
    database with the user accounts, etc), that would most likely warrant an
    exception because the error was of an unexpected nature.

    The idea is to use an exception to notify you when something happens that
    should not happen within the normal operation of the method that throws it.

    In all truth the decision for when and when not to use exceptions is kind of
    subjective. It definitely depends upon what makes sense in the context of
    your task, but you should be aware that handling an exception is an
    expensive operation.

    "suzy" <com> wrote in message
    news:%235xY$phx.gbl... 
    how 


    J.Marsch Guest

  4. #4

    Default error handling in c#

    Hi

    Have you put the code in try/catach, in which u r calling
    c# class.

    Exceptiption class is the base class for all exceptions.
    When an error occurs, either the system or the currently
    executing application reports it by throwing an exception
    containing information about the error. Once thrown, an
    exception is handled by the application or by the default
    exception handler.

    Check out this URL:
    http://samples.gotdotnet.com/quickstart/util/srcview.aspx?
    path=%2fquickstart%2fhowto%2fsamples%2fexceptions% 2fthrow%
    2fthrow.src

    Ravikanth[MVP]

     
    process requires a 
    the username and 
    password is entered i 
    error message. (i 
    is invalid is: 
    makes the aspx stop 
    the aspx page. how 
    can display it? 
    Ravikanth[MVP] Guest

  5. #5

    Default Re: error handling in c#

    thanks,

    so how can i make my login method return an error string to the calling
    function?

    if i write my code using your example, i will need to write an error message
    everytime i call the login function? is there a way i can write my code so
    i only have to write the error in the login function, and calling functions
    will pick up the error message if the login fails?


    "J.Marsch" <com> wrote in message
    news:%phx.gbl... 
    e.Message 
    to 
    SQL 
    it. 
    of [/ref]
    and [/ref]
    (i [/ref]
    stop 
    > how 
    >
    >[/ref]


    suzy Guest

  6. #6

    Default Re: error handling in c#

    You really want to avoid throwing Exceptions on purpose. This is a rather
    expensive process, and in most cases can be avoided by good code. For
    example, why don't you just write a function that returns true if successful
    and false if not? Then you can just check the return value of the function,
    and react accordingly.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    http://www.takempis.com
    Neither a follower nor a lender be.

    "suzy" <com> wrote in message
    news:%235xY$phx.gbl... 
    how 


    Kevin Guest

  7. #7

    Default Re: error handling in c#

    Well, rather than throwing a Boolean, you could have the Login function
    return some type of error indicator.
    In general, I would tend to shy away from having it return a string, because
    that makes it a little more difficult to handle errors programmatically.

    If you are trying to give the user hints (i.e: invalid password, account is
    locked out, etc)
    You could have it return a numerical constant, or better yet, you could have
    it return an enum. Something like:
    public enum LoginResult {loginSuccessful, loginBadPassword,
    loginAccountLockedOut}

    Then, in your login function:
    public LoginResult Login(string userName, string password)
    {
    // if the login works return LoginResult.loginSuccessful
    // if not, return one of the others
    }


    "suzy" <com> wrote in message
    news:phx.gbl... 
    message 
    so 
    functions 
    > e.Message [/ref]
    recieve 
    > to [/ref]
    user's [/ref]
    bad [/ref]
    to 
    > SQL [/ref]
    that 
    > it. [/ref]
    kind [/ref]
    of [/ref][/ref]
    a [/ref]
    > and [/ref][/ref]
    i [/ref]
    > (i [/ref]
    > stop 
    > > how 
    > >
    > >[/ref]
    >
    >[/ref]


    J.Marsch Guest

Similar Threads

  1. Error handling
    By Allen Browne in forum PHP Programming
    Replies: 31
    Last Post: April 17th, 07:35 AM
  2. CF Error Handling
    By JakeFlynn in forum Macromedia ColdFusion
    Replies: 0
    Last Post: March 16th, 01:29 AM
  3. better error handling...?
    By Kaosweaver in forum Macromedia Dreamweaver
    Replies: 0
    Last Post: July 17th, 02:15 AM
  4. Error handling and custom error messages
    By Cedomir Markovic in forum Microsoft SQL / MS SQL Server
    Replies: 1
    Last Post: July 10th, 09:00 AM
  5. Error handling in SQL
    By George Ter-Saakov in forum Microsoft SQL / MS SQL Server
    Replies: 1
    Last Post: July 7th, 05:33 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139