Handle ArgumentOutOfRangeException : Predefined Exception : Language Basics C# Examples


C# Examples » Language Basics » Predefined Exception »

 

Handle ArgumentOutOfRangeException









    
using  System;
using  System.Collections;
using  System.Runtime.CompilerServices;

[assembly:  RuntimeCompatibility(WrapNonExceptionThrows  =  false)]
public  class  MainClass
{
        static  void  Main()  {
                try  {
                        ArrayList  list  =  new  ArrayList();
                        list.Add(  1  );

                        Console.WriteLine(  "Item  10  =  {0}",  list[10]  );
                }
                catch(  ArgumentOutOfRangeException  x  )  {
                        Console.WriteLine(  x  );
                        Console.WriteLine(  "ArgumentOutOfRangeException  Handler"  );
                }
                catch(  Exception  x  )  {
                        Console.WriteLine(  x  );
                        Console.WriteLine(  "Exception  Handler"  );
                }
                catch  {
                        Console.WriteLine(  "An  exception  I  was  not  expecting..."  );
                        Console.WriteLine(  "Unexpected  Exception  Handler"  );
                }
                finally  {
                        Console.WriteLine(  "Cleaning  up..."  );
                }
        }
}
    
   
  
   



Output

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the s
ize of the collection.
Parameter name: index
   at System.Collections.ArrayList.get_Item(Int32 index)
   at MainClass.Main()
ArgumentOutOfRangeException Handler
Cleaning up...


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Language Basics
» Predefined Exception