473,561 Members | 3,555 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 1867
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
3258
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
4447
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...
2
9865
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...
1
2505
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...
3
2884
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...
0
11559
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...
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
6712
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
3284
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...
0
7639
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...
0
7855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8074
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...
1
7608
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...
0
3613
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...
0
3594
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2058
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
1
1172
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
885
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...

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.