Skip to content

Commit 5c07791

Browse files
authored
Fix formatting and usage of raise in F# exceptions
Follow-up to #43 from @codeconscious
1 parent f5a493b commit 5c07791

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

docs/fsharp-cheatsheet.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ See [Pattern Matching (MS Learn)](https://learn.microsoft.com/en-us/dotnet/fsha
689689

690690
## Try..With
691691

692-
An illustrative example with: custom F# exception creation, all exception aliases, `raise()` usage, and an exhaustive demonstration of the exception handler patterns:
692+
An illustrative example with: custom F# exception creation, all exception aliases, `raise` usage, and an exhaustive demonstration of the exception handler patterns:
693693

694694
```fsharp
695695
open System
@@ -702,26 +702,26 @@ try
702702
invalidArg "ArgumentName" "Message" // throws a System.ArgumentException
703703
invalidOp "Message" // throws a System.InvalidOperation
704704
705-
raise(NotImplementedException("Message")) // throws a .NET exception (2)
706-
raise(MyException(0, "Message")) // throws an F# exception (2)
705+
raise (NotImplementedException("Message")) // throws a .NET exception (2)
706+
raise (MyException(0, "Message")) // throws an F# exception (2)
707707
708708
true // (3)
709709
with
710710
| :? ArgumentNullException -> printfn "NullException"; false // (3)
711711
| :? ArgumentException as ex -> printfn $"{ex.Message}"; false // (4)
712-
| :? InvalidOperationException as ex when guard -> printfn $"{ex.Message}"; reraise() // (5,6)
712+
| :? InvalidOperationException as ex when guard -> printfn $"{ex.Message}"; reraise () // (5,6)
713713
| MyException(num, str) when guard -> printfn $"{num}, {str}"; false // (5)
714-
| MyException(num, str) -> printfn $"{num}, {str}"; reraise() // (6)
714+
| MyException(num, str) -> printfn $"{num}, {str}"; reraise () // (6)
715715
| ex when guard -> printfn $"{ex.Message}"; false
716716
| ex -> printfn $"{ex.Message}"; false
717717
```
718718

719719
1. define your own F# exception types with `exception`, a new type that will inherit from `System.Exception`;
720-
2. use `raise()` to throw an F# or .NET exception;
720+
2. use `raise` to throw (an F# or .NET) exception;
721721
3. the entire `try..with` expression must evaluate to the same type, in this example: bool;
722722
4. `ArgumentNullException` inherits from `ArgumentException`, so `ArgumentException` must follow after;
723723
5. support for `when` guards;
724-
6. use `reraise()` to re-throw an exception; works with both .NET and F# exceptions
724+
6. use `reraise ()` to re-throw the exception being handled (retaining the original throw site in the stack trace); works with both .NET and F# exceptions
725725

726726
The difference between F# and .NET exceptions is how they are created and how they can be handled.
727727

0 commit comments

Comments
 (0)