Error Handling


by Chris Kemp - Date: 2007-01-25 - Word Count: 870 Share This!

For years, the weak part of most of the languages supported by Microsoft has been the error handling capability. In most cases this error handling was a variant of 2 statements:
On Error Goto; and
On Error Resume Next
Which resulted in branching to an End statement, or continuing on as if nothing had happened.
Taking a page from the Java model, Microsoft, in order to achieve cross platform uniformity with all of the .Net languages, has adopted the
Try..Catch..Finally..End Try method of handling errors. This allows an error to be handled in the routine in which it occurs. If the exception is not caught, it is referred to the calling procedure, and so on and so on. If no exception is caught by the end of the calling chain, the user is notified by a dialog box.
Exception objects have 4 properties in common. The Message property is the text which describes the error. It is similar to the old Err.description. StackTrace returns a string which traces the path from where the exception was originally thrown. The Source property returns s the name of the routine where the exception was thrown. This is analogous to Err. Source. HelpLink returns the URL or URN to the helpfile associated with the error.
Some of the more common exceptions are listed in the following table:
System.AppDomainUnloadedException
Thrown when attempting to use an unloaded application domain.
System.ApplicationException
Thrown when a non-fatal application error has occurred
System.ArgumentException
Thrown when an argument passed is not valid.
System.ArgumentNullException
Thrown when a null is passed as a method parameter that does not accept null values.
System.ArgumentOutOfRangeException
Thrown when a passed value is outside the range of a methods parameter.
System.ArithmeticException
Thrown when an error occurs while performing arithmetic and conversion operations.
System.ArrayTypeMismatchException
Thrown when adding a value of the incorrect data type to an array.
System.DivideByZeroException
Thrown whenever a value is divided by zero.
System.DllNotFoundException
Thrown when a DLL referenced as imported is not available
System.IndexOutOfRangeException
Thrown when trying to access an invalid index in an array
System.InvalidCaseException
Thrown when an invalid conversion attempt is made.
System.NullReferenceException.
Thrown when attempting to dereference a null object reference
System.OutOfMemoryException
Thrown when memory is not available to perform the specified task.
System.OverflowException
Thrown when an operation overflow occurs
System.RankException
Thrown when an array with the wrong number of dimensions is passed to a methods parameter.
System.SystemException
Is the base class for all exception classes in the System namespace
System.Data.ConstraintException
Thrown when a constraint is violated.
System.Data.DataException.
Thrown when an ADO.NET component generates an error
System.Data.DBConcurrencyException
Thrown when the number of rows affected in an update procedure is zero.
System.Data. DeletedRowInaccessibleException
Thrown when attempting to perform data manipulation operations on a data row that has been deleted.
System.Data. InvalidConstraintException
Thrown when a data relationship is violated.
System.Data. NoNullAllowedException
Thrown when inserting a null value where one is not accepted.
System.IO. DirectoryNotFoundException
Thrown when a specified directory cannot be found.
System.IO.FileLoadException
Thrown when a file cannot be loaded.
System.IO.IOException.
Thrown when an I/O error occurs
System.IO.PathToLongException
Thrown when a path or file name are too long.
System.Runtime.Remoting..
Thrown when an error occurs during a remote RemotingException operation
System.Runtime.Remoting.
RemotingTimeoutException Thrown when the response of a server or client exceed a predefined interval.
System.Runtime.Remoting.ServerException
Thrown when an error occurs while working with a remote component that is an unmanaged application incapable of throwing an exception.
System.Runtime.Serialization.SerializationException
Thrown when an error occurs during the serialization or deserialization of a component.
System.Web.HttpException
Allow an http exception to be thrown
System.XML.XmlException
Provides exception information about the last XML exception.
 
Immediately following the TRY command, the code to be executed is placed. If an error is detected, control is passed immediately to the CATCH commands. The CATCH commands should be listed with the most restrictive listed first. There are in most cases multiple CATCH blocks to accommodate different types of errors and different resolutions for each. You can use s WHEN with a CATCH line to additionally specify conditions for that CATCH block to be executed. A TRY block can be exited at any time upon encountering an EXIT TRY command
Dim a,b as Integer
Try
a=a/b
b=1000000000000
Catch exn as DividebyZeroException
Console.WriteLine(“You tried to divide by zero”)
Catch exn as OverflowException ‘never executed
Console.WriteLine(“Number too big”)
 
Finally
Console.WriteLine(a,b)
End Try
 
 
The FINALLY clause is optional, and used to clean up any code or partially executed block of code which may have been interrupted. The code between FINALLY and ENDTRY is guaranteed to be executed whether or not an exception was thrown.
You can also THROW an exception (create your own error to trigger) by using the THROW keyword, and using the Namespace and Exception as a method, as in:
THROW New System.DividebyZeroException()
Try to use catching exception as sparingly as possible because they add overhead to the project. On the other hand, it is important to trap errors because in so doing you relieve the user of the burden of figuring out what went wrong. In addition, it is often a good idea to keep a database of errors to monitor the performance of the application over time.
Resources
Information on System.IndexOutOfRangeException Class
This resource provides information on System.IndexOutOfRangeException Class.
·         Information on System.ArgumentException Class
This resource provides information on System.ArgumentException Class.


Chris Kemp is a well knoen author who writes best quality articles on IT, Software, Programming, etc. For further details please visit the site www.paladn.com Your Article Search Directory : Find in Articles

© The article above is copyrighted by it's author. You're allowed to distribute this work according to the Creative Commons Attribution-NoDerivs license.
 

Recent articles in this category:



Most viewed articles in this category: