Skip to content

Commit af2fd41

Browse files
authored
Nullable: Annotate remaining System.*Exception (dotnet#23585)
* Nullable: Annotate remaining System.*Exception * PR feedback
1 parent aebc5ac commit af2fd41

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+188
-160
lines changed

src/System.Private.CoreLib/shared/System/AccessViolationException.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
**
1212
=============================================================================*/
1313

14+
#nullable enable
1415
using System;
1516
using System.Runtime.Serialization;
1617

@@ -26,13 +27,13 @@ public AccessViolationException()
2627
HResult = HResults.E_POINTER;
2728
}
2829

29-
public AccessViolationException(string message)
30+
public AccessViolationException(string? message)
3031
: base(message)
3132
{
3233
HResult = HResults.E_POINTER;
3334
}
3435

35-
public AccessViolationException(string message, Exception innerException)
36+
public AccessViolationException(string? message, Exception? innerException)
3637
: base(message, innerException)
3738
{
3839
HResult = HResults.E_POINTER;

src/System.Private.CoreLib/shared/System/AggregateException.cs

Lines changed: 14 additions & 13 deletions
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
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
56
using System;
67
using System.Collections.Generic;
78
using System.Collections.ObjectModel;
@@ -41,7 +42,7 @@ public AggregateException()
4142
/// a specified error message.
4243
/// </summary>
4344
/// <param name="message">The error message that explains the reason for the exception.</param>
44-
public AggregateException(string message)
45+
public AggregateException(string? message)
4546
: base(message)
4647
{
4748
m_innerExceptions = new ReadOnlyCollection<Exception>(Array.Empty<Exception>());
@@ -55,7 +56,7 @@ public AggregateException(string message)
5556
/// <param name="innerException">The exception that is the cause of the current exception.</param>
5657
/// <exception cref="T:System.ArgumentNullException">The <paramref name="innerException"/> argument
5758
/// is null.</exception>
58-
public AggregateException(string message, Exception innerException)
59+
public AggregateException(string? message, Exception innerException)
5960
: base(message, innerException)
6061
{
6162
if (innerException == null)
@@ -104,10 +105,10 @@ public AggregateException(params Exception[] innerExceptions) :
104105
/// is null.</exception>
105106
/// <exception cref="T:System.ArgumentException">An element of <paramref name="innerExceptions"/> is
106107
/// null.</exception>
107-
public AggregateException(string message, IEnumerable<Exception> innerExceptions)
108+
public AggregateException(string? message, IEnumerable<Exception> innerExceptions)
108109
// If it's already an IList, pass that along (a defensive copy will be made in the delegated ctor). If it's null, just pass along
109110
// null typed correctly. Otherwise, create an IList from the enumerable and pass that along.
110-
: this(message, innerExceptions as IList<Exception> ?? (innerExceptions == null ? (List<Exception>)null : new List<Exception>(innerExceptions)))
111+
: this(message, innerExceptions as IList<Exception> ?? (innerExceptions == null ? (List<Exception>)null! : new List<Exception>(innerExceptions)))
111112
{
112113
}
113114

@@ -121,7 +122,7 @@ public AggregateException(string message, IEnumerable<Exception> innerExceptions
121122
/// is null.</exception>
122123
/// <exception cref="T:System.ArgumentException">An element of <paramref name="innerExceptions"/> is
123124
/// null.</exception>
124-
public AggregateException(string message, params Exception[] innerExceptions) :
125+
public AggregateException(string? message, params Exception[] innerExceptions) :
125126
this(message, (IList<Exception>)innerExceptions)
126127
{
127128
}
@@ -135,7 +136,7 @@ public AggregateException(string message, params Exception[] innerExceptions) :
135136
/// is null.</exception>
136137
/// <exception cref="T:System.ArgumentException">An element of <paramref name="innerExceptions"/> is
137138
/// null.</exception>
138-
private AggregateException(string message, IList<Exception> innerExceptions)
139+
private AggregateException(string? message, IList<Exception> innerExceptions)
139140
: base(message, innerExceptions != null && innerExceptions.Count > 0 ? innerExceptions[0] : null)
140141
{
141142
if (innerExceptions == null)
@@ -195,7 +196,7 @@ internal AggregateException(string message, IEnumerable<ExceptionDispatchInfo> i
195196
// null typed correctly. Otherwise, create an IList from the enumerable and pass that along.
196197
: this(message, innerExceptionInfos as IList<ExceptionDispatchInfo> ??
197198
(innerExceptionInfos == null ?
198-
(List<ExceptionDispatchInfo>)null :
199+
(List<ExceptionDispatchInfo>)null! :
199200
new List<ExceptionDispatchInfo>(innerExceptionInfos)))
200201
{
201202
}
@@ -257,7 +258,7 @@ protected AggregateException(SerializationInfo info, StreamingContext context) :
257258
throw new ArgumentNullException(nameof(info));
258259
}
259260

260-
Exception[] innerExceptions = info.GetValue("InnerExceptions", typeof(Exception[])) as Exception[];
261+
Exception[]? innerExceptions = info.GetValue("InnerExceptions", typeof(Exception[])) as Exception[];
261262
if (innerExceptions == null)
262263
{
263264
throw new SerializationException(SR.AggregateException_DeserializationFailure);
@@ -292,14 +293,14 @@ public override Exception GetBaseException()
292293
// Returns the first inner AggregateException that contains more or less than one inner exception
293294

294295
// Recursively traverse the inner exceptions as long as the inner exception of type AggregateException and has only one inner exception
295-
Exception back = this;
296-
AggregateException backAsAggregate = this;
296+
Exception? back = this;
297+
AggregateException? backAsAggregate = this;
297298
while (backAsAggregate != null && backAsAggregate.InnerExceptions.Count == 1)
298299
{
299-
back = back.InnerException;
300+
back = back!.InnerException;
300301
backAsAggregate = back as AggregateException;
301302
}
302-
return back;
303+
return back!;
303304
}
304305

305306
/// <summary>
@@ -338,7 +339,7 @@ public void Handle(Func<Exception, bool> predicate)
338339
throw new ArgumentNullException(nameof(predicate));
339340
}
340341

341-
List<Exception> unhandledExceptions = null;
342+
List<Exception>? unhandledExceptions = null;
342343
for (int i = 0; i < m_innerExceptions.Count; i++)
343344
{
344345
// If the exception was not handled, lazily allocate a list of unhandled

src/System.Private.CoreLib/shared/System/ApplicationException.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
**
1313
=============================================================================*/
1414

15+
#nullable enable
1516
using System.Runtime.Serialization;
1617

1718
namespace System
@@ -39,13 +40,13 @@ public ApplicationException()
3940
// message, its HRESULT set to COR_E_APPLICATION,
4041
// and its ExceptionInfo reference set to null.
4142
//
42-
public ApplicationException(string message)
43+
public ApplicationException(string? message)
4344
: base(message)
4445
{
4546
HResult = HResults.COR_E_APPLICATION;
4647
}
4748

48-
public ApplicationException(string message, Exception innerException)
49+
public ApplicationException(string? message, Exception? innerException)
4950
: base(message, innerException)
5051
{
5152
HResult = HResults.COR_E_APPLICATION;

src/System.Private.CoreLib/shared/System/ArithmeticException.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
**
1212
=============================================================================*/
1313

14+
#nullable enable
1415
using System.Runtime.Serialization;
1516

1617
namespace System
@@ -34,13 +35,13 @@ public ArithmeticException()
3435
// message, its HRESULT set to COR_E_ARITHMETIC,
3536
// and its ExceptionInfo reference set to null.
3637
//
37-
public ArithmeticException(string message)
38+
public ArithmeticException(string? message)
3839
: base(message)
3940
{
4041
HResult = HResults.COR_E_ARITHMETIC;
4142
}
4243

43-
public ArithmeticException(string message, Exception innerException)
44+
public ArithmeticException(string? message, Exception? innerException)
4445
: base(message, innerException)
4546
{
4647
HResult = HResults.COR_E_ARITHMETIC;

src/System.Private.CoreLib/shared/System/ArrayTypeMismatchException.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
**
1212
=============================================================================*/
1313

14+
#nullable enable
1415
using System.Runtime.Serialization;
1516

1617
namespace System
@@ -34,13 +35,13 @@ public ArrayTypeMismatchException()
3435
// message, its HRESULT set to COR_E_ARRAYTYPEMISMATCH,
3536
// and its ExceptionInfo reference set to null.
3637
//
37-
public ArrayTypeMismatchException(string message)
38+
public ArrayTypeMismatchException(string? message)
3839
: base(message)
3940
{
4041
HResult = HResults.COR_E_ARRAYTYPEMISMATCH;
4142
}
4243

43-
public ArrayTypeMismatchException(string message, Exception innerException)
44+
public ArrayTypeMismatchException(string? message, Exception? innerException)
4445
: base(message, innerException)
4546
{
4647
HResult = HResults.COR_E_ARRAYTYPEMISMATCH;

src/System.Private.CoreLib/shared/System/BadImageFormatException.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
**
1212
===========================================================*/
1313

14+
#nullable enable
1415
using System.Globalization;
1516
using System.IO;
1617
using System.Runtime.Serialization;
@@ -21,34 +22,34 @@ namespace System
2122
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
2223
public partial class BadImageFormatException : SystemException
2324
{
24-
private string _fileName; // The name of the corrupt PE file.
25-
private string _fusionLog = null; // fusion log (when applicable)
25+
private string? _fileName; // The name of the corrupt PE file.
26+
private string? _fusionLog; // fusion log (when applicable)
2627

2728
public BadImageFormatException()
2829
: base(SR.Arg_BadImageFormatException)
2930
{
3031
HResult = HResults.COR_E_BADIMAGEFORMAT;
3132
}
3233

33-
public BadImageFormatException(string message)
34+
public BadImageFormatException(string? message)
3435
: base(message)
3536
{
3637
HResult = HResults.COR_E_BADIMAGEFORMAT;
3738
}
3839

39-
public BadImageFormatException(string message, Exception inner)
40+
public BadImageFormatException(string? message, Exception? inner)
4041
: base(message, inner)
4142
{
4243
HResult = HResults.COR_E_BADIMAGEFORMAT;
4344
}
4445

45-
public BadImageFormatException(string message, string fileName) : base(message)
46+
public BadImageFormatException(string? message, string? fileName) : base(message)
4647
{
4748
HResult = HResults.COR_E_BADIMAGEFORMAT;
4849
_fileName = fileName;
4950
}
5051

51-
public BadImageFormatException(string message, string fileName, Exception inner)
52+
public BadImageFormatException(string? message, string? fileName, Exception? inner)
5253
: base(message, inner)
5354
{
5455
HResult = HResults.COR_E_BADIMAGEFORMAT;
@@ -74,7 +75,7 @@ public override string Message
7475
get
7576
{
7677
SetMessageField();
77-
return _message;
78+
return _message!;
7879
}
7980
}
8081

@@ -91,7 +92,7 @@ private void SetMessageField()
9192
}
9293
}
9394

94-
public string FileName
95+
public string? FileName
9596
{
9697
get { return _fileName; }
9798
}
@@ -121,7 +122,7 @@ public override string ToString()
121122
return s;
122123
}
123124

124-
public string FusionLog
125+
public string? FusionLog
125126
{
126127
get { return _fusionLog; }
127128
}

src/System.Private.CoreLib/shared/System/CannotUnloadAppDomainException.cs

Lines changed: 3 additions & 2 deletions
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
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
56
using System.Runtime.Serialization;
67

78
namespace System
@@ -20,13 +21,13 @@ public CannotUnloadAppDomainException()
2021
HResult = COR_E_CANNOTUNLOADAPPDOMAIN;
2122
}
2223

23-
public CannotUnloadAppDomainException(string message)
24+
public CannotUnloadAppDomainException(string? message)
2425
: base(message)
2526
{
2627
HResult = COR_E_CANNOTUNLOADAPPDOMAIN;
2728
}
2829

29-
public CannotUnloadAppDomainException(string message, Exception innerException)
30+
public CannotUnloadAppDomainException(string? message, Exception? innerException)
3031
: base(message, innerException)
3132
{
3233
HResult = COR_E_CANNOTUNLOADAPPDOMAIN;

src/System.Private.CoreLib/shared/System/DataMisalignedException.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
**
1010
=============================================================================*/
1111

12+
#nullable enable
1213
using System.Runtime.Serialization;
1314

1415
namespace System
@@ -23,13 +24,13 @@ public DataMisalignedException()
2324
HResult = HResults.COR_E_DATAMISALIGNED;
2425
}
2526

26-
public DataMisalignedException(string message)
27+
public DataMisalignedException(string? message)
2728
: base(message)
2829
{
2930
HResult = HResults.COR_E_DATAMISALIGNED;
3031
}
3132

32-
public DataMisalignedException(string message, Exception innerException)
33+
public DataMisalignedException(string? message, Exception? innerException)
3334
: base(message, innerException)
3435
{
3536
HResult = HResults.COR_E_DATAMISALIGNED;

src/System.Private.CoreLib/shared/System/DivideByZeroException.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
**
1212
=============================================================================*/
1313

14+
#nullable enable
1415
using System.Runtime.Serialization;
1516

1617
namespace System
@@ -25,13 +26,13 @@ public DivideByZeroException()
2526
HResult = HResults.COR_E_DIVIDEBYZERO;
2627
}
2728

28-
public DivideByZeroException(string message)
29+
public DivideByZeroException(string? message)
2930
: base(message)
3031
{
3132
HResult = HResults.COR_E_DIVIDEBYZERO;
3233
}
3334

34-
public DivideByZeroException(string message, Exception innerException)
35+
public DivideByZeroException(string? message, Exception? innerException)
3536
: base(message, innerException)
3637
{
3738
HResult = HResults.COR_E_DIVIDEBYZERO;

src/System.Private.CoreLib/shared/System/DllNotFoundException.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
**
1313
=============================================================================*/
1414

15+
#nullable enable
1516
using System.Runtime.Serialization;
1617

1718
namespace System
@@ -26,13 +27,13 @@ public DllNotFoundException()
2627
HResult = HResults.COR_E_DLLNOTFOUND;
2728
}
2829

29-
public DllNotFoundException(string message)
30+
public DllNotFoundException(string? message)
3031
: base(message)
3132
{
3233
HResult = HResults.COR_E_DLLNOTFOUND;
3334
}
3435

35-
public DllNotFoundException(string message, Exception inner)
36+
public DllNotFoundException(string? message, Exception? inner)
3637
: base(message, inner)
3738
{
3839
HResult = HResults.COR_E_DLLNOTFOUND;

0 commit comments

Comments
 (0)