Csharp/CSharp Tutorial/Network/Network Exception

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

Handle network exceptions

using System; 
using System.Net; 
using System.IO; 
 
class MainClass {  
  public static void Main() { 
    int ch; 
 
    try { 
 
      HttpWebRequest req = (HttpWebRequest) WebRequest.Create("http://www.nfex.ru"); 
 
      HttpWebResponse resp = (HttpWebResponse) req.GetResponse(); 
 
      Stream istrm = resp.GetResponseStream(); 
 
      for(int i=1; ; i++) { 
        ch =  istrm.ReadByte(); 
        if(ch == -1) 
           break; 
        Console.Write((char) ch); 
      } 
 
      resp.Close(); 
 
    } catch(WebException exc) { 
      Console.WriteLine("Network Error: " + exc.Message +  
                        "\nStatus code: " + exc.Status); 
    } catch(ProtocolViolationException exc) { 
      Console.WriteLine("Protocol Error: " + exc.Message); 
    } catch(UriFormatException exc) { 
      Console.WriteLine("URI Format Error: " + exc.Message); 
    } catch(NotSupportedException exc) { 
      Console.WriteLine("Unknown Protocol: " + exc.Message); 
    } catch(IOException exc) { 
      Console.WriteLine("I/O Error: " + exc.Message); 
    } catch(System.Security.SecurityException exc) { 
      Console.WriteLine("Security Exception: " + exc.Message); 
    } catch(InvalidOperationException exc) { 
      Console.WriteLine("Invalid Operation: " + exc.Message); 
    } 
  } 
}