473,467 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Handling exceptions

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.
Nov 19 '05 #1
3 1121
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.
Nov 19 '05 #2
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.


Nov 19 '05 #3

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.


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
28
by: Frank Puck | last post by:
Meanwhile there are at least 8 years that compilers exist, which provide a working implementation of C++ Exception Handling. Has anything changed meanwhile? From my point of view nothing has...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
6
by: Jesper Ordrup Christensen | last post by:
Say I've created a piece of code that involves both sql, io and some number conversions. Being a responsible developer I have tried to catch all of the exceptions - but how can I be sure? Is...
34
by: rawCoder | last post by:
I have read that Exception Handling is expensive performance wise ( other than Throw ) , but exactly how ? Please consider the following example ... ////////////////// Code Block 1...
2
by: Rajeev Soni | last post by:
Hi, Considering the scenario for handling exceptions in Web Application where we have Presentation layer, Business layer and Data Access layer; if there any exception is occurred in DAL, what is...
16
by: Chuck Cobb | last post by:
I'm implementing a centralized exception handling routine using the Enterprise Library Exception Management Application Block. I trap all unhandled exceptions to one place using the following...
5
by: Bry | last post by:
I've created a class that offers an enhanced way of handling fatal exceptions. The class allows the user to optionaly submit a http based anonymous error report to myself, and also records details...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
35
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.