Handling exceptions

Discussion in 'ASP .Net' started by Stephen Witter, Dec 5, 2004.

  1. I have the following example to catch more than one error. I am new
    to error handling in .net so I am wondering if the following is an
    exceptable approach:
    Sub LinkButton1_Click(sender As Object, e As EventArgs)

    Try
    txtDate.text=("hi"/0)
    Catch ex As Exception
    if ex.GetType.ToString ="System.InvalidCastException" then
    Response.write("You have entered an invalid date.")
    else
    Response.write("There was an error.")
    end if
    exit sub
    End Try
    End Sub

    I am used to error handling in access where, using the err.number, you
    could trap a specific error and return a message for that error. The
    only reason I ask is because the above approach wasn't real obvious to
    find at MSDN so I question whether I should be doing it this way. Any
    suggestions are appreciated.
    Stephen Witter, Dec 5, 2004
    #1
    1. Advertising

  2. Proper exception handling is a topic in its own, but in your case
    I would strongly suggest that you handle the possibility of a
    InvalidCastException as follows.

    Try
    txtDate.Text = ("hi" / 0)
    ....
    Catch ex As InvalidCastException
    'Code to handle the specific exception
    Response.write("You have entered an invalid date.")
    Catch ex1 As Exception
    'Code to handle all other exceptions
    Response.write("There was an error.")
    End Try

    The "Try ...Catch" construct is designed to give the possibility of handling
    each type of exception uniquely. The catch-construct will do the necessary
    type checking for us, such that we should never have to write code like

    > Catch ex As Exception
    > if ex.GetType.ToString ="System.InvalidCastException" then
    >


    It sounds that what you try to do is actually input validation. Best
    practise tend to discourage the use of exceptions for this purpose. Chances
    are you should consider employing a validation scheme (such as .NET built-in
    validators).

    Proper exception handling in .NET is a new school of thought compared to
    error handling in Access/VB.
    You may benefit from further reading on the subject, such as
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/
    emab-rm.asp

    Tor Bådshaug
    tor.badshaug [//at\\] bekk.no.
    Tor Bådshaug, Dec 5, 2004
    #2
    1. Advertising

  3. Stephen Witter

    Stephen Guest

    That is precisely what I was looking for. Thank you. I am flirting
    with validation controls as we speak.

    Tor Bådshaug wrote:
    > Proper exception handling is a topic in its own, but in your case
    > I would strongly suggest that you handle the possibility of a
    > InvalidCastException as follows.
    >
    > Try
    > txtDate.Text = ("hi" / 0)
    > ....
    > Catch ex As InvalidCastException
    > 'Code to handle the specific exception
    > Response.write("You have entered an invalid date.")
    > Catch ex1 As Exception
    > 'Code to handle all other exceptions
    > Response.write("There was an error.")
    > End Try
    >
    > The "Try ...Catch" construct is designed to give the possibility of

    handling
    > each type of exception uniquely. The catch-construct will do the

    necessary
    > type checking for us, such that we should never have to write code

    like
    >
    > > Catch ex As Exception
    > > if ex.GetType.ToString ="System.InvalidCastException"

    then
    > >

    >
    > It sounds that what you try to do is actually input validation. Best
    > practise tend to discourage the use of exceptions for this purpose.

    Chances
    > are you should consider employing a validation scheme (such as .NET

    built-in
    > validators).
    >
    > Proper exception handling in .NET is a new school of thought compared

    to
    > error handling in Access/VB.
    > You may benefit from further reading on the subject, such as
    >

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/
    > emab-rm.asp
    >
    > Tor Bådshaug
    > tor.badshaug [//at\\] bekk.no.
    Stephen, Dec 6, 2004
    #3
  4. Stephen Witter

    Stephen Guest

    Tor Bådshaug wrote:
    > Proper exception handling is a topic in its own, but in your case
    > I would strongly suggest that you handle the possibility of a
    > InvalidCastException as follows.
    >
    > Try
    > txtDate.Text = ("hi" / 0)
    > ....
    > Catch ex As InvalidCastException
    > 'Code to handle the specific exception
    > Response.write("You have entered an invalid date.")
    > Catch ex1 As Exception
    > 'Code to handle all other exceptions
    > Response.write("There was an error.")
    > End Try
    >
    > The "Try ...Catch" construct is designed to give the possibility of

    handling
    > each type of exception uniquely. The catch-construct will do the

    necessary
    > type checking for us, such that we should never have to write code

    like
    >
    > > Catch ex As Exception
    > > if ex.GetType.ToString ="System.InvalidCastException"

    then
    > >

    >
    > It sounds that what you try to do is actually input validation. Best
    > practise tend to discourage the use of exceptions for this purpose.

    Chances
    > are you should consider employing a validation scheme (such as .NET

    built-in
    > validators).
    >
    > Proper exception handling in .NET is a new school of thought compared

    to
    > error handling in Access/VB.
    > You may benefit from further reading on the subject, such as
    >

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/
    > emab-rm.asp
    >
    > Tor Bådshaug
    > tor.badshaug [//at\\] bekk.no.
    Stephen, Dec 6, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raterus
    Replies:
    6
    Views:
    564
    Raterus
    Aug 24, 2004
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    29,972
    Chris Smith
    Jul 14, 2004
  3. Paul Miller
    Replies:
    3
    Views:
    997
    Alex Martelli
    Nov 12, 2003
  4. Replies:
    3
    Views:
    584
    Sherm Pendley
    Apr 16, 2007
  5. Lie
    Replies:
    3
    Views:
    591
Loading...

Share This Page