Handling exceptions

S

Stephen Witter

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.
 
T

Tor Bådshaug

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.
 
S

Stephen

That is precisely what I was looking for. Thank you. I am flirting
with validation controls as we speak.
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.
 
S

Stephen

Tor said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top