471,855 Members | 1,124 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,855 software developers and data experts.

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...Finally 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
In error raising we'll use the Throw command.

2- Try...Catch...Finally Block

The Try...Catch...Finally Block structure

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

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

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.

' ----- The Code is here.
Catch ex As System.OutOfMemoryException
' ----- Handle memory errors here.
Catch ex As System.Exception
----- 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
' ----- 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.Text) 'Here when converting
Dim b As
Integer = CInt(TextBox2.Text) '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 1755
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.)

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Thelma Lubkin | last post: by
2 posts views Thread by Michael Mayer | last post: by
1 post views Thread by Clay Hobbs | last post: by
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by

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.