.net Net如何读取异常堆栈以了解要捕获的异常

.net Net如何读取异常堆栈以了解要捕获的异常,.net,exception,exception-handling,.net,Exception,Exception Handling,好的,我正在尝试根据已发生错误的历史记录向程序添加异常处理。但我正挣扎着到底该“抓住”什么样的例外。我在下面列出了3个不同的异常,其中2个是相同的,但我猜不同的.net版本对异常的详细程度不同 是否有一种“好”的方法来阅读这些内容,并知道要捕获哪些异常。在第三个示例中,它列出了CommunicationException、WebException、IOException和SocketException,因此应该使用哪一个来捕获并响应它。经验告诉我,CommunicationException过于

好的,我正在尝试根据已发生错误的历史记录向程序添加异常处理。但我正挣扎着到底该“抓住”什么样的例外。我在下面列出了3个不同的异常,其中2个是相同的,但我猜不同的.net版本对异常的详细程度不同

是否有一种“好”的方法来阅读这些内容,并知道要捕获哪些异常。在第三个示例中,它列出了CommunicationException、WebException、IOException和SocketException,因此应该使用哪一个来捕获并响应它。经验告诉我,CommunicationException过于宽泛,涵盖了太多我不想捕捉的异常

System.ServiceModel.CommunicationException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. ---> System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.

at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
--- End of inner exception stack trace ---
这是:

System.ServiceModel.CommunicationException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. ---> System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
--- End of inner exception stack trace ---
或者这个:

System.ServiceModel.CommunicationException: An error occurred while receiving the HTTP response to http://webaddress/WebServices/webservice.asmx. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
--- End of inner exception stack trace ---

在我看来,它们都像是
CommunicationException
s。你为什么不想抓住那个例外?如果有“太多的异常”听起来像是一个完全不同的问题。还有许多其他的异常来自于通信异常,比如FaultException,还有一些不是由于连接速度慢或断开造成的。但我的问题是如何解读和识别“真正”的例外。