Some common used exception classes

Don't throw general exceptions

There is a common rule to provide the user with sufficient information about the error. To do this don't use the following, more general exception classes because they don't provide the user enough information about the exception:

  • Exception
  • Application Exception
  • System Exception

Don't throw reserved exceptions

There are some reserverd exception wich are thrown by the CLR. Keep things seperated so, don't go throwing with these.

  • ExecutionEngineException
  • IndexOutOfRangeException
  • NullReferenceException
  • OutOfMemoryException

So, what to use then?

  • AccessException - Failure to access a type member, such as a method or field.
  • ArgumentException - An argument to a method was invalid.
  • ArgumentNullException - A null argument was passed to a method that doesn't accept it.
  • ArgumentOutOfRangeException - Argument value is out of range.
  • ArithmeticException - Arithmetic over - or underflow has occurred.
  • ArrayTypeMismatchException - Attempt to store the wrong type of object in an array.
  • BadImageFormatException - Image is in the wrong format.
  • CoreException - Base class for exceptions thrown by the runtime.
  • DivideByZeroException - An attempt was made to divide by zero.
  • FormatException - The format of an argument is wrong.
  • IndexOutOfRangeException - An array index is out of bounds.
  • InvalidCastExpression - An attempt was made to cast to an invalid class.
  • InvalidOperationException - A method was called at an invalid time.
  • MissingMemberException - An invalid version of a DLL was accessed.
  • NotFiniteNumberException - A number is not valid.
  • NotSupportedException - Indicates sthat a method is not implemented by a class.
  • NullReferenceException - Attempt to use an unassigned reference.
  • StackOverflowException - A stack has overflown.

And otherwise, build you own:

It's very simple to build you're own exception. Why not do that and provide the user with lots of information about their error. It will look a bit like this:

Public class CustomException:Exception
    // constructor needs to be defined
    Public CustomException(string error):base (error){}





Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!