Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Handling exceptions

Reply
Thread Tools

Handling exceptions

 
 
Stephen Witter
Guest
Posts: n/a
 
      12-05-2004
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.
 
Reply With Quote
 
 
 
 
Tor Bådshaug
Guest
Posts: n/a
 
      12-05-2004
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/de...us/dnbda/html/
emab-rm.asp

Tor Bådshaug
tor.badshaug [//at\\] bekk.no.


 
Reply With Quote
 
 
 
 
Stephen
Guest
Posts: n/a
 
      12-06-2004
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/de...us/dnbda/html/
> emab-rm.asp
>
> Tor Bådshaug
> tor.badshaug [//at\\] bekk.no.


 
Reply With Quote
 
Stephen
Guest
Posts: n/a
 
      12-06-2004

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/de...us/dnbda/html/
> emab-rm.asp
>
> Tor Bådshaug
> tor.badshaug [//at\\] bekk.no.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exceptions - How do you make it work like built-in exceptions? Lie Python 3 01-14-2008 06:45 PM
Exceptions + Performance on path without exceptions gratch06@gmail.com C++ 3 04-16-2007 08:52 PM
Handling Exceptions in an n-tier environment Raterus ASP .Net 6 08-24-2004 08:21 PM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments
 



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