Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System.Runtime.ExceptionServices;
using System.Threading;

namespace System.Net
Expand Down Expand Up @@ -147,9 +148,14 @@ internal void Complete(HttpListenerContext context, bool synch)
internal HttpListenerContext GetContext()
{
if (_forward != null)
{
return _forward.GetContext();
}

if (_exception != null)
throw _exception;
{
ExceptionDispatchInfo.Capture(_exception).Throw();
}

return _context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Net.Security;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Authentication.ExtendedProtection;
Expand Down Expand Up @@ -949,7 +950,7 @@ public HttpListenerContext EndGetContext(IAsyncResult asyncResult)
if (httpContext == null)
{
Debug.Assert(castedAsyncResult.Result is Exception, "EndGetContext|The result is neither a HttpListenerContext nor an Exception.");
throw castedAsyncResult.Result as Exception;
ExceptionDispatchInfo.Capture(castedAsyncResult.Result as Exception).Throw();
}
}
catch (Exception exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.IO;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -357,7 +358,7 @@ public override int EndRead(IAsyncResult asyncResult)
NetEventSource.Info(this, "Rethrowing exception:" + exception);
NetEventSource.Error(this, exception.ToString());
}
throw exception;
ExceptionDispatchInfo.Capture(exception).Throw();
}

uint dataRead = (uint)returnValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.IO;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Globalization;
Expand Down Expand Up @@ -366,7 +367,7 @@ public override void EndWrite(IAsyncResult asyncResult)
if (NetEventSource.IsEnabled) NetEventSource.Error(this, "Rethrowing exception:" + exception);
_closed = true;
_httpContext.Abort();
throw exception;
ExceptionDispatchInfo.Capture(exception).Throw();
}

if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
Expand Down
5 changes: 3 additions & 2 deletions src/System.Net.Mail/src/System/Net/Mail/MailPriority.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Net.Mime;
using System.Runtime.ExceptionServices;
using System.Text;

namespace System.Net.Mail
Expand Down Expand Up @@ -372,9 +373,9 @@ internal virtual void EndSend(IAsyncResult asyncResult)

castedAsyncResult.InternalWaitForCompletion();
castedAsyncResult.EndCalled = true;
if (castedAsyncResult.Result is Exception)
if (castedAsyncResult.Result is Exception e)
{
throw (Exception)castedAsyncResult.Result;
ExceptionDispatchInfo.Capture(e).Throw();
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/System.Net.Mail/src/System/Net/Mail/SmtpCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Globalization;
using System.IO;
using System.Net.Mime;
using System.Runtime.ExceptionServices;
using System.Text;

namespace System.Net.Mail
Expand Down Expand Up @@ -46,8 +47,10 @@ internal static IAsyncResult BeginSend(SmtpConnection conn, AsyncCallback callba
internal static object EndSend(IAsyncResult result, out string response)
{
object commandResult = MultiAsyncResult.End(result);
if (commandResult is Exception)
throw (Exception)commandResult;
if (commandResult is Exception e)
{
ExceptionDispatchInfo.Capture(e).Throw();
}

LineInfo info = (LineInfo)commandResult;
response = info.Line;
Expand Down Expand Up @@ -135,8 +138,10 @@ internal static IAsyncResult BeginSend(SmtpConnection conn, AsyncCallback callba
internal static LineInfo[] EndSend(IAsyncResult result)
{
object commandResult = MultiAsyncResult.End(result);
if (commandResult is Exception)
throw (Exception)commandResult;
if (commandResult is Exception e)
{
ExceptionDispatchInfo.Capture(e).Throw();
}
return (LineInfo[])commandResult;
}

Expand Down
9 changes: 5 additions & 4 deletions src/System.Net.Mail/src/System/Net/Mail/SmtpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Runtime.ExceptionServices;
using System.Security.Authentication;
using System.Security.Authentication.ExtendedProtection;
using System.Security.Cryptography.X509Certificates;
Expand Down Expand Up @@ -221,7 +222,7 @@ internal void GetConnection(string host, int port)
if ((e.StatusCode != SmtpStatusCode.CommandUnrecognized)
&& (e.StatusCode != SmtpStatusCode.CommandNotImplemented))
{
throw e;
throw;
}

HelloCommand.Send(this, _client.clientDomain);
Expand Down Expand Up @@ -453,9 +454,9 @@ internal static void End(IAsyncResult result)
{
ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result;
object connectResult = thisPtr.InternalWaitForCompletion();
if (connectResult is Exception)
if (connectResult is Exception e)
{
throw (Exception)connectResult;
ExceptionDispatchInfo.Capture(e).Throw();
}
}

Expand Down Expand Up @@ -641,7 +642,7 @@ private static void SendEHelloCallback(IAsyncResult result)
if ((e.StatusCode != SmtpStatusCode.CommandUnrecognized)
&& (e.StatusCode != SmtpStatusCode.CommandNotImplemented))
{
throw e;
throw;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw retains the information but not throw e? Why don't we need the Capture/Throw here?

Copy link
Copy Markdown
Member Author

@stephentoub stephentoub Mar 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. throw; can only be used inside of a catch block, and effectively continues the propagation of the exception that was caught (kind of like catch-and-release'ing a fish), so it retains its original info. throw e; can be done anywhere, and overwrites the info.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha, thanks for the explanation.

}

if (!thisPtr.SendHello())
Expand Down
5 changes: 3 additions & 2 deletions src/System.Net.Mail/src/System/Net/Mail/SmtpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Net.Mime;
using System.Runtime.ExceptionServices;
using System.Security.Cryptography.X509Certificates;

namespace System.Net.Mail
Expand Down Expand Up @@ -318,11 +319,11 @@ internal static MailWriter End(IAsyncResult result)

// Note the difference between the singular and plural FailedRecipient exceptions.
// Only fail immediately if we couldn't send to any recipients.
if ((sendMailResult is Exception)
if ((sendMailResult is Exception e)
&& (!(sendMailResult is SmtpFailedRecipientException)
|| ((SmtpFailedRecipientException)sendMailResult).fatal))
{
throw (Exception)sendMailResult;
ExceptionDispatchInfo.Capture(e).Throw();
}

return new MailWriter(thisPtr._stream);
Expand Down
5 changes: 3 additions & 2 deletions src/System.Net.Mail/src/System/Net/Mime/BaseWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Collections.Specialized;
using System.Net.Mail;
using System.Runtime.ExceptionServices;

namespace System.Net.Mime
{
Expand Down Expand Up @@ -146,9 +147,9 @@ internal IAsyncResult BeginGetContentStream(AsyncCallback callback, object state
internal Stream EndGetContentStream(IAsyncResult result)
{
object o = MultiAsyncResult.End(result);
if (o is Exception)
if (o is Exception e)
{
throw (Exception)o;
ExceptionDispatchInfo.Capture(e).Throw();
}
return (Stream)o;
}
Expand Down
3 changes: 2 additions & 1 deletion src/System.Net.Mail/src/System/Net/Mime/MimeMultiPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Runtime.ExceptionServices;
using System.Threading;

namespace System.Net.Mime
Expand Down Expand Up @@ -62,7 +63,7 @@ internal void Complete(IAsyncResult result, Exception e)

if (context._completed)
{
throw e;
ExceptionDispatchInfo.Capture(e).Throw();
}

try
Expand Down
3 changes: 2 additions & 1 deletion src/System.Net.Mail/src/System/Net/Mime/MimePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections;
using System.Globalization;
using System.Net.Mail;
using System.Runtime.ExceptionServices;

namespace System.Net.Mime
{
Expand Down Expand Up @@ -161,7 +162,7 @@ internal void Complete(IAsyncResult result, Exception e)
MimePartContext context = (MimePartContext)result.AsyncState;
if (context._completed)
{
throw e;
ExceptionDispatchInfo.Capture(e).Throw();
}

try
Expand Down
3 changes: 2 additions & 1 deletion src/System.Net.NameResolution/src/System/Net/DNS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Globalization;
using System.Net.Internals;
using System.Net.Sockets;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -416,7 +417,7 @@ private static IPHostEntry HostResolutionEndHelper(IAsyncResult asyncResult)
Exception exception = castedResult.Result as Exception;
if (exception != null)
{
throw exception;
ExceptionDispatchInfo.Capture(exception).Throw();
}

return (IPHostEntry)castedResult.Result;
Expand Down
7 changes: 5 additions & 2 deletions src/System.Net.Requests/src/System/Net/FtpDataStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.IO;
using System.Net.Sockets;
using System.Runtime.ExceptionServices;

namespace System.Net
{
Expand Down Expand Up @@ -255,8 +256,10 @@ public override int EndRead(IAsyncResult ar)
{
object result = ((LazyAsyncResult)ar).InternalWaitForCompletion();

if (result is Exception)
throw (Exception)result;
if (result is Exception e)
{
ExceptionDispatchInfo.Capture(e).Throw();
}

return (int)result;
}
Expand Down
3 changes: 2 additions & 1 deletion src/System.Net.Requests/src/System/Net/FtpWebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Net.Cache;
using System.Net.Sockets;
using System.Security;
using System.Runtime.ExceptionServices;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
Expand Down Expand Up @@ -1231,7 +1232,7 @@ private void CheckError()
{
if (_exception != null)
{
throw _exception;
ExceptionDispatchInfo.Capture(_exception).Throw();
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/System.Net.Security/src/System/Net/Security/NegoState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Security.Principal;
using System.Threading;
using System.ComponentModel;
using System.Runtime.ExceptionServices;
using System.Security.Authentication;
using System.Security.Authentication.ExtendedProtection;

Expand Down Expand Up @@ -112,7 +113,7 @@ internal void ValidateCreateContext(
{
if (_exception != null && !_canRetryAuthentication)
{
throw _exception;
ExceptionDispatchInfo.Capture(_exception).Throw();
}

if (_context != null && _context.IsValidContext)
Expand Down Expand Up @@ -313,7 +314,7 @@ internal void CheckThrow(bool authSucessCheck)
{
if (_exception != null)
{
throw _exception;
ExceptionDispatchInfo.Capture(_exception).Throw();
}

if (authSucessCheck && !IsAuthenticated)
Expand Down Expand Up @@ -398,7 +399,7 @@ internal void EndProcessAuthentication(IAsyncResult result)
{
// Round-trip it through the SetException().
e = SetException(e);
throw e;
ExceptionDispatchInfo.Capture(e).Throw();
}
}

Expand Down Expand Up @@ -689,7 +690,7 @@ private void StartSendAuthResetSignal(LazyAsyncResult lazyResult, byte[] message
}

_canRetryAuthentication = true;
throw exception;
ExceptionDispatchInfo.Capture(exception).Throw();
}

private static void WriteCallback(IAsyncResult transportResult)
Expand All @@ -713,10 +714,10 @@ private static void WriteCallback(IAsyncResult transportResult)
authState._framer.EndWriteMessage(transportResult);

// Special case for an error notification.
if (lazyResult.Result is Exception)
if (lazyResult.Result is Exception e)
{
authState._canRetryAuthentication = true;
throw (Exception)lazyResult.Result;
ExceptionDispatchInfo.Capture(e).Throw();
}

authState.CheckCompletionBeforeNextReceive(lazyResult);
Expand Down
Loading