473,385 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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
block.
In error raising we'll use the Throw command.

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

The Try...Catch...Finally 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.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
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.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 1856
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
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...
13
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...
2
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...
1
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...
3
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...
0
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...
0
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...
1
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

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.