VB.NET Technology

VB.Net Projects

VB.Net Project 1

What is Exception
Previous Home Next
adplus-dvertising

The exceptions are anomalies that occur during the execution of a program. Exception handling is a mechanism in .NET framework to detect and handle run time errors. They can be because of user, logic or system errors. If a user (programmer) do not provide a mechanism to handle these anomalies, the .NET run time environment provide a default mechanism, which terminates the program execution.

In VB.Net there are three keywords Try, Catch and Finally for handling exceptions. In try lock statements it might throw an exception whereas catch handles that caused by try block if one exists. The finally block is used for doing any clean up process. The statement in finally block always executes.

Example

' this can cause an exception.
Try
		' for handling the exception.
Catch x As Type
		'this will execute.
Finally
End Try

Handling Exceptions

In catch block, if don't use a brackets or arguments, we can catch all exceptions occurred inside a try block. Even we can use a catch block with an Exception type parameter to catch all exceptions happened inside the try block.

Since in VB.net, all exceptions are directly or indirectly inherited from the Exception class.

Example

Class newexp
	Public Shared Sub Main()
		Dim a As Integer = 0
		Dim div As Integer = 0
		Try
			div = 100 \ a
			Console.WriteLine("This will not print")
		Catch
			Console.WriteLine("oException")
		End Try
		Console.WriteLine("Result is {0}", div)
	End Sub
End Class

Exceptions Classes

Following are some common exception classes.

  • SystemException : This exception means a failed run-time check used as a base class for other.
  • AccessException : This exception means failure to access a type member, such as a method or field.
  • ArgumentException : This exception means an argument to a method was invalid.
  • ArgumentNullException : This exception means a null argument was passed to a method that doesn't accept it.
  • ArgumentOutOfRangeException :This exception means argument value is out of range.
  • ArithmeticException : This exception means arithmetic over – or underflow has occurred.
  • ArrayTypeMismatchException : This exception means attempt to store the wrong type of object in an array.
  • BadImageFormatException : This exception means image is in the wrong format.
  • CoreException : This exception means base class for exceptions thrown by the runtime.
  • DivideByZeroException : This exception means an attempt was made to divide by zero.
  • FormatException : This exception means the format of an argument is wrong.
  • IndexOutOfRangeException : This exception means an array index is out of bounds.
  • InvalidCastExpression : This exception means an attempt was made to cast to an invalid class.
  • InvalidOperationException : This exception means a method was called at an invalid time.
  • MissingMemberException : This exception means an invalid version of a DLL was accessed.
  • NotFiniteNumberException : This exception means a number is not valid.
  • NotSupportedException : This exception means indicates that a method is not implemented by a class.
  • NullReferenceException : This exception means attempt to use an unassigned reference.
  • OutOfMemoryException : This exception means not enough memory to continue execution.
  • StackOverflowException : This exception means a stack has overflow.
Previous Home Next