Skip to content

Commit aade354

Browse files
Use ExceptionDispatchInfo in more places (#1182)
Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
1 parent 18e6673 commit aade354

5 files changed

+19
-9
lines changed

src/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using System.Runtime.ExceptionServices;
34
using System.Threading;
45

56
using Renci.SshNet.Abstractions;
@@ -78,7 +79,7 @@ public override AuthenticationResult Authenticate(Session session)
7879

7980
if (_exception != null)
8081
{
81-
throw _exception;
82+
ExceptionDispatchInfo.Capture(_exception).Throw();
8283
}
8384

8485
return _authenticationResult;

src/Renci.SshNet/PasswordAuthenticationMethod.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.ExceptionServices;
23
using System.Text;
34
using System.Threading;
45

@@ -114,7 +115,7 @@ public override AuthenticationResult Authenticate(Session session)
114115

115116
if (_exception != null)
116117
{
117-
throw _exception;
118+
ExceptionDispatchInfo.Capture(_exception).Throw();
118119
}
119120

120121
return _authenticationResult;

src/Renci.SshNet/Sftp/SftpFileReader.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Globalization;
4+
using System.Runtime.ExceptionServices;
45
using System.Threading;
56

67
using Renci.SshNet.Abstractions;
@@ -80,7 +81,7 @@ public byte[] Read()
8081

8182
if (_exception is not null)
8283
{
83-
throw _exception;
84+
ExceptionDispatchInfo.Capture(_exception).Throw();
8485
}
8586

8687
if (_isEndOfFileRead)
@@ -102,7 +103,7 @@ public byte[] Read()
102103
// throw when exception occured in read-ahead, or the current instance is already disposed
103104
if (_exception != null)
104105
{
105-
throw _exception;
106+
ExceptionDispatchInfo.Capture(_exception).Throw();
106107
}
107108

108109
var data = nextChunk.Data;

src/Renci.SshNet/SshCommand.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Globalization;
33
using System.IO;
4+
using System.Runtime.ExceptionServices;
45
using System.Text;
56
using System.Threading;
67

@@ -483,7 +484,8 @@ private void WaitOnHandle(WaitHandle waitHandle)
483484
switch (signaledElement)
484485
{
485486
case 0:
486-
throw _exception;
487+
ExceptionDispatchInfo.Capture(_exception).Throw();
488+
break;
487489
case 1:
488490
// Specified waithandle was signaled
489491
break;

src/Renci.SshNet/SubsystemSession.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Globalization;
3+
using System.Runtime.ExceptionServices;
34
using System.Threading;
45

56
using Renci.SshNet.Abstractions;
@@ -241,7 +242,8 @@ public void WaitOnHandle(WaitHandle waitHandle, int millisecondsTimeout)
241242
switch (result)
242243
{
243244
case 0:
244-
throw _exception;
245+
ExceptionDispatchInfo.Capture(_exception).Throw();
246+
break;
245247
case 1:
246248
throw new SshException("Connection was closed by the server.");
247249
case 2:
@@ -286,7 +288,8 @@ public bool WaitOne(WaitHandle waitHandle, int millisecondsTimeout)
286288
switch (result)
287289
{
288290
case 0:
289-
throw _exception;
291+
ExceptionDispatchInfo.Capture(_exception).Throw();
292+
return false; // unreached
290293
case 1:
291294
throw new SshException("Connection was closed by the server.");
292295
case 2:
@@ -340,7 +343,8 @@ public int WaitAny(WaitHandle waitHandle1, WaitHandle waitHandle2, int milliseco
340343
switch (result)
341344
{
342345
case 0:
343-
throw _exception;
346+
ExceptionDispatchInfo.Capture(_exception).Throw();
347+
return -1; // unreached
344348
case 1:
345349
throw new SshException("Connection was closed by the server.");
346350
case 2:
@@ -377,7 +381,8 @@ public int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout)
377381
switch (result)
378382
{
379383
case 0:
380-
throw _exception;
384+
ExceptionDispatchInfo.Capture(_exception).Throw();
385+
return -1; // unreached
381386
case 1:
382387
throw new SshException("Connection was closed by the server.");
383388
case 2:

0 commit comments

Comments
 (0)