Handle network exceptions : Network Exception « Network « C# / CSharp Tutorial






using System; 
using System.Net; 
using System.IO; 
 
class MainClass {  
  public static void Main() { 
    int ch; 
 
    try { 
 
      HttpWebRequest req = (HttpWebRequest) WebRequest.Create("http://www.java2s.com"); 
 
      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); 
    } 
  } 
}








33.17.Network Exception
33.17.1.Handle network exceptions