Использование блоков Try-Catch

После ограничения недопустимых запросов к серверу отчетов путем добавления условных инструкций в код необходимо обеспечить соответствующую обработку исключений посредством использования блоков try-catch. Этот метод обеспечивает дополнительный уровень защиты от недопустимых запросов. Если запрос к серверу отчетов заключен в блок try, а затем в результате этого запроса сервер отчетов вызывает исключение, то исключение перехватывается в блоке catch, что предотвращает непредвиденное завершение работы приложения. Перехваченное исключение можно использовать, чтобы сообщить пользователю о необходимости изменить какое-либо действие или просто известить пользователя в понятной форме о том, что произошла ошибка. Затем с помощью блока finally можно очистить ресурсы. В идеальном случае необходимо выработать общий план по обработке исключений, чтобы избежать дублирования блоков try-catch.

В следующем примере блоки try-catch используются для повышения надежности кода, обрабатывающего события.

// C#private void PublishReport(){   int index;   string reservedChar;   string message;   // Check the text value of the name text box for "/",   // a reserved character   index = nameTextBox.Text.IndexOf(@"/");   if ( index != -1) // The text contains the character   {      reservedChar = nameTextBox.Text.Substring(index, 1);      // Build a user-friendly error message      message = "The name of the report cannot contain the reserved character " +         "\"" + reservedChar + "\". " +         "Please enter a valid name for the report. " +         "For more information about reserved characters, " +         "consult the online documentation";      MessageBox.Show(message, "Invalid Input Error");   }   else // Publish the report   {      Byte[] definition = null;      Warning[] warnings = null;      string name = nameTextBox.Text;      try      {         FileStream stream = File.OpenRead("MyReport.rdl");         definition = new Byte[stream.Length];         stream.Read(definition, 0, (int) stream.Length);         stream.Close();         // Create report with user-defined name         warnings = rs.CreateReport(name, "/Samples", false, definition, null);         MessageBox.Show("Report: {0} created successfully", name);      }      // Catch expected exceptions beginning with the most specific,      // moving to the least specific      catch(IOException ex)      {         MessageBox.Show(ex.Message, "File IO Exception");      }      catch (SoapException ex)      {         // The exception is a SOAP exception, so use         // the Detail property's Message element.         MessageBox.Show(ex.Detail["Message"].InnerXml, "SOAP Exception");       }      catch (Exception ex)      {         MessageBox.Show(ex.Message, "General Exception");      }   }}