Repository navigation
Expand file tree
/
Copy pathArgumentException.cs
More file actions
139 lines (121 loc) · 5.47 KB
/
Copy pathArgumentException.cs
File metadata and controls
139 lines (121 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
namespace System
{
/// <summary>
/// The exception that is thrown when one of the arguments provided to a method is not valid.
/// </summary>
[Serializable]
[TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public class ArgumentException : SystemException
{
private readonly string? _paramName;
// Creates a new ArgumentException with its message
// string set to the empty string.
public ArgumentException()
: base(SR.Arg_ArgumentException)
{
HResult = HResults.COR_E_ARGUMENT;
}
// Creates a new ArgumentException with its message
// string set to message.
//
public ArgumentException(string? message)
: base(message)
{
HResult = HResults.COR_E_ARGUMENT;
}
public ArgumentException(string? message, Exception? innerException)
: base(message, innerException)
{
HResult = HResults.COR_E_ARGUMENT;
}
public ArgumentException(string? message, string? paramName, Exception? innerException)
: base(message, innerException)
{
_paramName = paramName;
HResult = HResults.COR_E_ARGUMENT;
}
public ArgumentException(string? message, string? paramName)
: base(message)
{
_paramName = paramName;
HResult = HResults.COR_E_ARGUMENT;
}
[Obsolete(Obsoletions.LegacyFormatterImplMessage, DiagnosticId = Obsoletions.LegacyFormatterImplDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[EditorBrowsable(EditorBrowsableState.Never)]
protected ArgumentException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
_paramName = info.GetString("ParamName");
}
[Obsolete(Obsoletions.LegacyFormatterImplMessage, DiagnosticId = Obsoletions.LegacyFormatterImplDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[EditorBrowsable(EditorBrowsableState.Never)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("ParamName", _paramName, typeof(string));
}
public override string Message
{
get
{
SetMessageField();
string s = base.Message;
if (!string.IsNullOrEmpty(_paramName))
{
s += " " + SR.Format(SR.Arg_ParamName_Name, _paramName);
}
return s;
}
}
private void SetMessageField()
{
if (_message == null && HResult == HResults.COR_E_ARGUMENT)
{
_message = SR.Arg_ArgumentException;
}
}
public virtual string? ParamName => _paramName;
/// <summary>Throws an exception if <paramref name="argument"/> is null or empty.</summary>
/// <param name="argument">The string argument to validate as non-null and non-empty.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
/// <exception cref="ArgumentNullException"><paramref name="argument"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="argument"/> is empty.</exception>
public static void ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if (string.IsNullOrEmpty(argument))
{
ThrowNullOrEmptyException(argument, paramName);
}
}
/// <summary>Throws an exception if <paramref name="argument"/> is null, empty, or consists only of white-space characters.</summary>
/// <param name="argument">The string argument to validate.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
/// <exception cref="ArgumentNullException"><paramref name="argument"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="argument"/> is empty or consists only of white-space characters.</exception>
public static void ThrowIfNullOrWhiteSpace([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if (string.IsNullOrWhiteSpace(argument))
{
ThrowNullOrWhiteSpaceException(argument, paramName);
}
}
[DoesNotReturn]
private static void ThrowNullOrEmptyException(string? argument, string? paramName)
{
ArgumentNullException.ThrowIfNull(argument, paramName);
throw new ArgumentException(SR.Argument_EmptyString, paramName);
}
[DoesNotReturn]
private static void ThrowNullOrWhiteSpaceException(string? argument, string? paramName)
{
ArgumentNullException.ThrowIfNull(argument, paramName);
throw new ArgumentException(SR.Argument_EmptyOrWhiteSpaceString, paramName);
}
}
}