zoukankan      html  css  js  c++  java
  • c#.net内部异常捕获问题

    public static void Download()
            {
                
    int count = 0;
                
    try
                {
                    HttpWebRequest request 
    = (HttpWebRequest)WebRequest.Create("http://img.bimg.126.net/photo/c7bckFJxdJxSwQLIy76HTQ==/1755840904721658663.jpg");
                    
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        
    byte[] buffer = new byte[64];
                        
    string temppath = "g:\\1755840904721658663.jpg";

                        
    using (System.IO.Stream downloadStream = response.GetResponseStream())
                        {
                            
    using (FileStream saveFileStream = new FileStream(temppath, FileMode.Create, FileAccess.Write, FileShare.None))
                            {
                                
    int readSize = 0;
                                
    while (true)
                                {
                                    readSize 
    = downloadStream.Read(buffer, 0, buffer.Length);
                                    
    if (readSize <= 0break;
                                    saveFileStream.Write(buffer, 
    0, readSize);
                                }
                                Console.WriteLine(
    "弹出");
                            }
                        }
                    }
                }
                
    catch (System.Net.Sockets.SocketException ex)
                {
                    Console.WriteLine(
    "网络中断");
                }
                
    catch (Exception ex)
                {
                    
    //throw ex;
                    Console.WriteLine("异常类型:{0}", ex.GetType());
                    Console.WriteLine(
    "异常信息:{0}", ex.Message);
                    Console.WriteLine(
    "异常来源:{0}", ex.Source);
                    Console.WriteLine(
    "异常堆栈:{0}", ex.StackTrace);
                    Console.WriteLine(
    "内部异常:{0}", ex.InnerException);
                }
            }

    异常类型:System.IO.IOException
    异常信息:无法从传输连接中读取数据: 远程主机强迫关闭了一个现有的连接。。
    内部异常:System.Net.Sockets.SocketException (0x80004005): 远程主机强迫关闭了一
    个现有的连接。
      在 System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size,
     SocketFlags socketFlags)
      在 System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 s
    ize)

    IOException.InnerException是因为在读取Stream过程中,网络中断导致。

    所以代码中应该加上一个Catch IOException的处理。

    ...
    catch(IOException ex)
    {
       
    if(IOException.InnerException is System.Net.Sockets.SocketException)
           Console.WriteLine(
    "网络中断");
       
    else
           Console.WriteLine(ex.Message);
    }
    ...

  • 相关阅读:
    HDU 4278 Faulty Odometer 8进制转10进制
    hdu 4740 The Donkey of Gui Zhou bfs
    hdu 4739 Zhuge Liang's Mines 随机化
    hdu 4738 Caocao's Bridges tarjan
    Codeforces Gym 100187M M. Heaviside Function two pointer
    codeforces Gym 100187L L. Ministry of Truth 水题
    Codeforces Gym 100187K K. Perpetuum Mobile 构造
    codeforces Gym 100187J J. Deck Shuffling dfs
    codeforces Gym 100187H H. Mysterious Photos 水题
    windows服务名称不是单个单词的如何启动?
  • 原文地址:https://www.cnblogs.com/lizhao/p/1990428.html
Copyright © 2011-2022 走看看