473,668 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ 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_Cli ck(sender As Object, e As EventArgs)

Try
txtDate.text=(" hi"/0)
Catch ex As Exception
if ex.GetType.ToSt ring ="System.Invali dCastException" 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 1134
Proper exception handling is a topic in its own, but in your case
I would strongly suggest that you handle the possibility of a
InvalidCastExce ption as follows.

Try
txtDate.Text = ("hi" / 0)
....
Catch ex As InvalidCastExce ption
'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.ToSt ring ="System.Invali dCastException" 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
InvalidCastExce ption as follows.

Try
txtDate.Text = ("hi" / 0)
....
Catch ex As InvalidCastExce ption
'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.ToSt ring ="System.Invali dCastException" 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
InvalidCastExce ption as follows.

Try
txtDate.Text = ("hi" / 0)
....
Catch ex As InvalidCastExce ption
'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.ToSt ring ="System.Invali dCastException" 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
3198
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 indeed also true for our language of choice, Python. Its file type allows some extraordinary convenient access like: for line in open("blah"): handle_line(line)
28
2241
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 changed -- people are still using a coding style, which I call pre-C++-Exception-Handling. Is this different in your experience? What is your experience regarding this subject? I think that a C++ IOstream lib, which is by standard free of exceptions...
9
2534
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 the way of CPU usage), compared to the "normal" practise returning values. How true is this? Will using using exception handling, in general, be much less efficient than returning values, or less efficient at all? Just curious...
6
746
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 there a way to list exactly which exceptions that are not handlet? (design time) I could of cause ... use catch (Exception e) {...} as the final catch ... but I would prefer to now the "landscape" before doing that ..
34
2220
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 ////////////////////////////////// Try { for ( ... ){// Code that might throw exception} }catch(...){} //////////////////////////////////////////////////////////////////////////
2
5865
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 the best thing to do: 1. Dont catch the exception in DAL and let it prop up to the Application level and the Global.Application_Error event log it to any source and show let ASP.NET show custom error page provided in Web.Config file. OR 2....
16
2537
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 method: // --- Create an Exception Handler for Thread Exceptions ---------------- Application.ThreadException += new ThreadExceptionEventHandler(OnThreadException);
5
3827
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 in the application log. The static method is overloaded, and supports passing exceptions and/or strings just like throwing an exception.The class will also fall back to the standard exception handling if something goes wrong in my class. As an...
35
3770
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 gets about 3 or so. It also gets very awkward in nested loops, where you want to check for normal loop processing in the loop condition, not errors. Yes, you could put some generic exit flag in the loop condition, but when you're simply done if...
35
3499
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, especially as programs get large. Most of the issues of exceptions are not specific to Python, but I sometimes feel that Python makes them more acute because of the free-n- easy manner in which it employs exceptions for its own uses and allows users...
0
8462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8381
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8799
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8658
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5681
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4205
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4380
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2026
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.