Error Handling
- 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.
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
Recent articles in this category:
- Web Development Projects - Developers Tools Tips
Open source usually refers to software that is released with source code under a license that ensur - A Guide to Cnc Kits
Building a CNC machine means that you would require a lot of tools, parts or different components. - Xlphoto Printing, the Best Choice for Banner Printing and Poster Printing
Suppose, you have visited a tourist spot or a hill station; over there, you have witnessed some of - Reliable Software Programming
Software reliability is an important facet of software quality. It is defined as "the probability - Designing an Ecommerce Site
In a recent article I talked about Google AdSense placement based on eye-tracking research. Howeve - Asp.net Development Benefits
Rightway solution has been keenly looking out for. Microsoft ASP.NET is a free technology that allo - Choosing Offshore Software Development Company
To turn into successful and gain the full benefits of software outsourcing, you require preferring - E-commerce and Web Portal Development in Vietnam
VIETNAM, owing to its superior intellectual capital has emerged as the ultimate destination for - Types of Softwares
Computer software is a general term used to describe a collection of computer programs, procedur - Offshore it Out Sourcing Consultancy in Vietnam
Vendors who provide Off shore IT Outsourcing Consultancy services are referred to as outsourcing
Most viewed articles in this category:
- Oracle Applications (Oracle E-Business Suite) Customizations: What is it?
Oracle recommends every company that installs Oracle Applications to avoid any customization, but I - Offshore Outsourcing: An All Win Premise
The awareness that outsourcing can boost productivity sans forfeit of class has impelled MNC’s - Oracle E-Business Suite vs. Microsoft Dynamics AX - Axapta: LATAM and Brazil
In this small article we will only concentrate on Microsoft Dynamics Axapta (and will not consider M - SAP Business One vs. Microsoft Dynamics GP - highlights for consultant
In this small article we will not make side-by-side technical specs comparison. We would rathe - Microsoft Great Plains - licensing and product versions
Current Microsoft Business Solutions Great Plains has more that 10 years of history. Former Gr - OCR Software - Optical Character Recognition or Optical Crud Recognition?
Optical Character Recognition (OCR) refers to a software technology and processes that involve the t - Microsoft Dynamics AX - Axapta - technical notes plus customization
Axapta or current name Microsoft Dynamics AX has one of the newest ERP design and architecture among - Microsoft Dynamics AX-GP-NAV-CRM: trends and international recommendations
Microsoft Great Plains/Microsoft Dynamics GP, Microsoft Navision (former Attain) Microsoft Dynamics - Microsoft Moves to Small Business Accounting/Retail Market - stakes and thoughts
In this small article we will be looking at the new opportunities for Microsoft Small Business Serve - Microsoft Axapta - Dynamics AX: Brazilian Portuguese version - consultant highlights
Axapta/Microsoft Dynamics AX short overview. Navision Axapta was designed by Navision Software