ASP.Net/Language Basics/Catch Exception

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

Use try catch to handle error (C#)

<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 + "<br>");
      }
    }
    // Handler for index out of range exception
    catch (IndexOutOfRangeException ex) 
    {
       Response.Write("Error Occurred"+ "<br>" + ex.ToString() + "<br>");
    }
    // Handler for generic exception
    catch (Exception e) 
    {
       Response.Write("Generic Error Occurred" + "<br>");
    }
    finally 
    {
       Response.Write("The Page Execution is completed" + "<br>");
    }
  }
</script>
<%
  StructuredErrorHandling();
  Response.Write("Function call completed" + "<br>");
%>