System.Exception—catch an ArgumentOutOfRangeException

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
C# Code Snippets

C# Source Code Bank

See also …
edit

This C# code snippet first causes and then catches an ArgumentOutOfRangeException.

using System;
 
class ArgumentOutOfRangeExceptionExample
{
   public static void Main()
   {
      int[] fromArray = {42, 42};
      int[] toArray   = {0, 0};
      try
      {
         Array.Copy (fromArray, toArray, -1);
      }
      catch (ArgumentOutOfRangeException e)
      {
         Console.WriteLine(e);
      }
   }
}


 ArgumentOutOfRangeException example (program output)
System.ArgumentOutOfRangeException: Non-negative number required.
Parameter name: length
at System.Array.Copy(Array sourceArray, Int32 sourceIndex,
Array destinationArray, Int32 destinationIndex, Int32 length,
Boolean reliable)
at System.Array.Copy(Array sourceArray, Array destinationArray, Int32 length)
at ArgumentOutOfRangeExceptionExample.Main() in Program.cs:line 8



Personal tools