ASP.Net/Language Basics/Catch Exception

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Use try catch to handle error (C#)

   <source lang="csharp">

<script Language="c#" runat="server" >

 void StructuredErrorHandling ()
 {
   try
   {
     int [] array = new int[9];
     for(int intCounter=0; intCounter <= 9; intCounter++)
     {
       array[intCounter] = intCounter;
       Response.Write("The value of the counter is:" + intCounter + "
"); } } // Handler for index out of range exception catch (IndexOutOfRangeException ex) { Response.Write("Error Occurred"+ "
" + ex.ToString() + "
"); } // Handler for generic exception catch (Exception e) { Response.Write("Generic Error Occurred" + "
"); } finally { Response.Write("The Page Execution is completed" + "
"); } }

</script> <%

 StructuredErrorHandling();
 Response.Write("Function call completed" + "
");

%>

      </source>