Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The invalidArg
function generates an argument exception.
invalidArg parameter-name error-message-string
The parameter-name in the previous syntax is a string with the name of the parameter whose argument was invalid. The error-message-string is a literal string or a value of type string
. It becomes the Message
property of the exception object.
The exception generated by invalidArg
is a System.ArgumentException
exception. The following code illustrates the use of invalidArg
to throw an exception.
let months = [| "January"; "February"; "March"; "April";
"May"; "June"; "July"; "August"; "September";
"October"; "November"; "December" |]
let lookupMonth month =
if (month > 12 || month < 1)
then invalidArg (nameof month) (sprintf "Value passed in was %d." month)
months[month - 1]
printfn "%s" (lookupMonth 12)
printfn "%s" (lookupMonth 1)
printfn "%s" (lookupMonth 13)
The output is the following, followed by a stack trace (not shown).
December
January
System.ArgumentException: Value passed in was 13. (Parameter 'month')
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register now