473,770 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tutorial 8 : Error Handling

Reason of this project:
Error handling is one of the most difficult thing that may afford a
programmer. It isn't as easy as you think and handling errors in a
program some time can make errors occur!

Project details:
1- New solution for handling errors in .net
2- Try...Catch...F inally Block
3- Error handling sample

1- New solution for handling errors in .net

Errors handling had been improved with the new .net. The Try...End try
block can minimize code and simplify it.
However some old methods still alive with the .net 2.0 which can be
useful sometimes.
There's also some changes that we'll discuss.
From the first Visual Basic edition and also I remember it on QBasic,
there was a method to handle errors which is "On Error".
Now the method still exist but replace with the new Try...End try
block.
In error raising we'll use the Throw command.

2- Try...Catch...F inally Block

The Try...Catch...F inally Block structure

Try
Here's the code where you want to monitor for errors

Catch
When an error occurs, the application will jump immediately to this
section.

Finally
This step is optional, but if defined it'll run whatever an error
occur or not.
Generally we use it to close an opened file or connection

There can be different types of errors.
This structure allows you to handle each type alone.


Try
' ----- The Code is here.
Catch ex As System.OutOfMem oryException
' ----- Handle memory errors here.
Catch ex As System.Exceptio n
'
----- Handle all other errors here.
End Try
You can do more customization for the Catch block and this shows how
much the .net is flexible.
Let understand how this was done.
The integer is empty when we start running the code. Now we go to step
1, the integer contain 1. If an error occur, the application will stop
processing and jump to the Catch blocks. Each block to choose? ;)


Dim Step as integer
Try
' ----- The Code is here.
Step = 1
.....
Step = 2
.....
Catch ex As Exception When Step = 1
' ----- Handle
the step 1 errors
Catch ex As Exception When Step = 2
' ----- Handle the
step 2 errors
End Try
3- Error handling sample

The sample is simple!
There's two text boxes, you have to fill both of them with numbers. By
pressing the button you'll divide them.
When the problem can occur?

Dim a As Integer = CInt(TextBox1.T ext) 'Here when converting
Dim b As
Integer = CInt(TextBox2.T ext) 'Here when converting
TextBox3.Text = a / b
'When dividing by Zero or big numbers (out of capacities)

How to handle those errors?
Check out the sample to know how we can do it. The sample is commented
and explanation is inside.
Subscribe to get the latest tutorials
Download the tutorial

The Zip file contains:
-The sample source code
-The readme.txt file
-The tutorial.txt file

Still have question:
Post a comment describing your problem.
If you have a general question, we highly recommend the MSDN Forums as
the best Dot Net forums in the net.
Jun 27 '08 #1
2 1884
On Jun 4, 1:16 pm, Omar Abid <omar.abid2...@ gmail.comwrote:
Reason of this project:
Error handling is one of the most difficult thing that may afford a
programmer. It isn't as easy as you think and handling errors in a
program some time can make errors occur!
It will certainly make errors occur at compile-time if you start
including VB code in C# code...

(Please don't make VB-specific posts on a C#-specific newsgroup.)

Jon
Jun 27 '08 #2
SPAM by any other name
Jun 27 '08 #3

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

Similar topics

2
3273
by: WSeeger | last post by:
When creating a new class, is it encouraged to always include error handling routines within your LET and GET procedures? It's seems that most text books never seem to include much about error handling within classes. Just hoping to hear some programmer's thoughts on error handling.
13
4485
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently runs into problems on the system where it will actually be used, and since I used so little error-trapping it dies very ungracefully. I will of course try to fix whatever is causing the error and add error-trapping to the functions where the...
2
9877
by: Michael Mayer | last post by:
This is not an ad for the best c# tutorial on the internet! And not from anybody world known. I just wanted to start a thread (to possibly gather more interest) based on a post Jon Skeet made earlier: <quote from Jon Skeet - skeet@pobox.com > I'm considering writing a C# tutorial. Any comments (including "don't do it, there are plenty out there already") are welcome - and if I *do* go ahead with it, I'd love it to
1
2511
by: maciek | last post by:
Hi, I was wondering if anyone could suggest me a book/article/tutorial on Exception Handling in multi tier Windows Apps. What are the best practices/ways to implement EH in multi tier enviroment. I read some MS Best Practices articles on MSDN. It helped a little, but considering how unexperienced programmer I am, I think I'd need a good sample code and explanation I could follow to fully understand the issue. Thanks in advance.
3
2891
by: Harry Zoroc | last post by:
I would like to learn some XSLT-FO handling procedures and need a good tutorial. Unfortunately most of the links in Google refer to books, lessons, XML syntax What I need is an easy step-by-step guide for installing and applying all necessary tools for converting a simple lets say "Hello world" XML doc into pdf with XSL-FO Does someone know such a quick start guide ? Harry
0
11599
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access Security, but I'll start with something short and simple This code was written in Access 2003 but should be valid in Access 2000 By default, when you start a new module, either in a form or report, or a global module, Access does not declare Option...
0
231
by: TonyJ | last post by:
Hello! I use VS2005 and C#. I'm looking at ADO.NET and have found some test tutorial on microsoft MSDN. It can be found on google when searching for "Walkthrough: Saving Data to a Database (Single Table)" You must have access to the Northwind sample database.
1
6741
by: Clay Hobbs | last post by:
I need a tutorial for PyOpenGL (specifically, to be used with wxPython). I searched with Google and didn't find one. Does anybody know where one is? -- Ratfink
9
3297
by: MrDeej | last post by:
Hello guys! We have an SQL server which sometimes makes timeouts and connection errors. And we have an function witch writes and updates data in 2 tables on this server. When the SQL server error appears it, in 99%, of the cases, works if we just press the play button in VBA debug. Therefor we have maked an error handling which just tryes again. However, as this error handling is difficult to test because of maybe 1 or 2 errors a day, we...
0
9617
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
9454
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
10099
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...
1
10037
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7456
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.