Skip to content

Commit

Permalink
Fix exception throwing inconsistency (dotnet#17328)
Browse files Browse the repository at this point in the history
* Fix exception throwing inconsitency

* release notes

* Update 8.0.400.md
  • Loading branch information
psfinaki committed Jun 25, 2024
1 parent 41cd736 commit b25b426
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/release-notes/.FSharp.Core/8.0.400.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@

* Cache delegate in query extensions. ([PR #17130](https://github.com/dotnet/fsharp/pull/17130))
* Update `AllowNullLiteralAttribute` to also use `AttributeTargets.Interface` ([PR #17173](https://github.com/dotnet/fsharp/pull/17173))

### Breaking Changes

* Fixed argument exception throwing inconsistency - accessing an out-of-bounds collection index will now throw `ArgumentOutOfRangeException` instead of `ArgumentException` ([#17328](https://github.com/dotnet/fsharp/pull/17328))
10 changes: 7 additions & 3 deletions src/FSharp.Core/local.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ module internal DetailedExceptions =
let msg = String.Format (format, paramArray)
raise (new ArgumentException (msg, arg))

/// takes an argument, a formatting string, a param array to splice into the formatting string
let inline invalidArgOutOfRangeFmt (arg:string) (format:string) paramArray =
let msg = String.Format (format, paramArray)
raise (new ArgumentOutOfRangeException (arg, msg))

/// takes a formatting string and a param array to splice into the formatting string
let inline invalidOpFmt (format:string) paramArray =
let msg = String.Format (format, paramArray)
Expand All @@ -38,7 +43,6 @@ module internal DetailedExceptions =
"{0}\nThe list was {1} {2} shorter than the index"
[|SR.GetString SR.notEnoughElements; index; (if index=1 then "element" else "elements")|]


/// eg. tried to {skip} {2} {elements} past the end of the seq. Seq.Length = {10}
let invalidOpExceededSeqLength (fnName:string) (diff:int) (len:int) =
invalidOpFmt "{0}\ntried to {1} {2} {3} past the end of the seq\nSeq.Length = {4}"
Expand All @@ -52,11 +56,11 @@ module internal DetailedExceptions =
let inline invalidArgInputMustBePositive (arg:string) (count:int) =
invalidArgFmt arg "{0}\n{1} = {2}" [|SR.GetString SR.inputMustBePositive; arg; count|]

/// throws an invalid argument exception and returns the out of range index,
/// throws an invalid argument out of range exception and returns the out of range index,
/// a text description of the range, and the bound of the range
/// e.g. sourceIndex = -4, source axis-0 lower bound = 0"
let invalidArgOutOfRange (arg:string) (index:int) (text:string) (bound:int) =
invalidArgFmt arg
invalidArgOutOfRangeFmt arg
"{0}\n{1} = {2}, {3} = {4}"
[|SR.GetString SR.outOfRange; arg; index; text; bound|]

Expand Down

0 comments on commit b25b426

Please sign in to comment.