Skip to content

Commit defe9ae

Browse files
Copilotleculver
andauthored
Change all bare catch blocks to handle specific relevant exceptions (#1328)
* Initial plan * Fix all bare catch blocks with specific exception types Co-authored-by: leculver <[email protected]> * Address PR feedback: use try/finally pattern in DataTarget.cs and revert global.json Co-authored-by: leculver <[email protected]> * Add missing System.IO imports for IOException and InvalidDataException Co-authored-by: leculver <[email protected]> * Add missing System.IO import for IOException and InvalidDataException in ResourceEntry.cs Co-authored-by: leculver <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: leculver <[email protected]>
1 parent 33412cd commit defe9ae

File tree

9 files changed

+18
-12
lines changed

9 files changed

+18
-12
lines changed

src/Microsoft.Diagnostics.Runtime/ClrModule.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Buffers;
66
using System.Collections.Generic;
77
using System.Diagnostics;
8+
using System.IO;
89
using System.Linq;
910
using System.Threading;
1011

@@ -268,7 +269,7 @@ private ulong GetSize()
268269
}
269270
}
270271
}
271-
catch
272+
catch (Exception ex) when (ex is IOException or ArgumentException or OutOfMemoryException or InvalidDataException or ObjectDisposedException)
272273
{
273274
}
274275

src/Microsoft.Diagnostics.Runtime/ClrRuntime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ private DomainAndModules InitAppDomainData()
466466
if (fileName.Equals(bclName, StringComparison.OrdinalIgnoreCase))
467467
bcl = module;
468468
}
469-
catch
469+
catch (Exception ex) when (ex is ArgumentException or IOException or NotSupportedException)
470470
{
471471
}
472472
}

src/Microsoft.Diagnostics.Runtime/DataReaders/LinuxSnapshot/LinuxSnapshotTarget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected override void Dispose(bool disposing)
2727
{
2828
File.Delete(_filename);
2929
}
30-
catch
30+
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException or NotSupportedException)
3131
{
3232
}
3333
}

src/Microsoft.Diagnostics.Runtime/DataTarget.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ public IEnumerable<ModuleInfo> EnumerateModules()
281281
/// <returns>A <see cref="DataTarget"/> for the given dump.</returns>
282282
public static DataTarget LoadDump(string displayName, Stream stream, CacheOptions? cacheOptions = null, bool leaveOpen = false, TokenCredential? symbolCredential = null)
283283
{
284+
bool success = false;
284285
try
285286
{
286287
if (displayName is null)
@@ -313,13 +314,14 @@ public static DataTarget LoadDump(string displayName, Stream stream, CacheOption
313314
_ => throw new InvalidDataException($"Stream '{displayName}' is in an unknown or unsupported file format."),
314315
};
315316

316-
return new DataTarget(new CustomDataTarget(reader, symbolCredential) { CacheOptions = cacheOptions });
317+
DataTarget result = new DataTarget(new CustomDataTarget(reader, symbolCredential) { CacheOptions = cacheOptions });
318+
success = true;
319+
return result;
317320
}
318-
catch
321+
finally
319322
{
320-
if (!leaveOpen)
323+
if (!success && !leaveOpen)
321324
stream?.Dispose();
322-
throw;
323325
}
324326
}
325327

src/Microsoft.Diagnostics.Runtime/GCRoot.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private void WorkerThread(CancellationToken token, BlockingCollection<ClrRoot> q
8181
queue.Add(root);
8282
}
8383
}
84-
catch
84+
catch (Exception ex) when (ex is InvalidOperationException or ObjectDisposedException or ArgumentException)
8585
{
8686
}
8787
finally

src/Microsoft.Diagnostics.Runtime/Implementation/DotNetClrInfoProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ protected ClrInfo CreateClrInfo(DataTarget dataTarget, ModuleInfo module, ulong
240240
}
241241
}
242242
}
243-
catch
243+
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException or NotSupportedException)
244244
{
245245
}
246246
}

src/Microsoft.Diagnostics.Runtime/Implementation/PEModuleInfo.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.IO;
56
using System.Threading;
67
using Microsoft.Diagnostics.Runtime.Utilities;
78

@@ -49,7 +50,7 @@ internal sealed class PEModuleInfo : ModuleInfo
4950

5051
image.Dispose();
5152
}
52-
catch
53+
catch (Exception ex) when (ex is IOException or ArgumentException or OutOfMemoryException or InvalidDataException or ObjectDisposedException)
5354
{
5455
}
5556

src/Microsoft.Diagnostics.Runtime/ModuleInfo.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Immutable;
6+
using System.IO;
67
using System.Linq;
78
using System.Runtime.CompilerServices;
89
using Microsoft.Diagnostics.Runtime.Implementation;
@@ -54,7 +55,7 @@ public abstract class ModuleInfo
5455
return new MacOS.MachOModuleInfo(module, baseAddress, name, null, module.ImageSize);
5556
}
5657
}
57-
catch
58+
catch (Exception ex) when (ex is InvalidDataException or IOException or ArgumentException or OverflowException or OutOfMemoryException)
5859
{
5960
// We could encounter any number of errors in the Elf/Mach-O system, we will just ignore them here.
6061
}

src/Microsoft.Diagnostics.Runtime/Utilities/PEImage/ResourceEntry.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Buffers;
66
using System.Collections.Immutable;
7+
using System.IO;
78
using System.Linq;
89
using System.Runtime.CompilerServices;
910
using System.Runtime.InteropServices;
@@ -165,7 +166,7 @@ public ImmutableArray<IResourceNode> Children
165166

166167
return _children = result.MoveOrCopyToImmutable();
167168
}
168-
catch
169+
catch (Exception ex) when (ex is OutOfMemoryException or OverflowException or InvalidDataException or ArgumentException or IOException)
169170
{
170171
// If there's a bad image we could hit a variety of different failures here, including out of memory or
171172
// under/overflow issues. We'll just not return anything if we hit an error here since a bad image

0 commit comments

Comments
 (0)