Created
May 5, 2024 10:36
-
-
Save FH-Inway/34984cfacc39b27e206873048a16fb3e to your computer and use it in GitHub Desktop.
.Net Interactive Polyglot notebook with "invalid" cell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Exceptions\n", | |
"\n", | |
"**Syntax for throwing and catching**\n", | |
"\n", | |
"Just like other .NET languages, F# supports throwing and catching exceptions. As with the [control flow expressions](./control-flow-expressions.ipynb), the syntax will feel familiar, but again there are a few catches that you should know about.\n", | |
"\n", | |
"## Defining your own exceptions\n", | |
"\n", | |
"When raising/throwing exceptions, you can use the standard system ones such as `InvalidOperationException`, or you can define your own exception types using the simple syntax shown below, where the \"content\" of the exception is any F# type:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"dotnet_interactive": { | |
"language": "fsharp" | |
}, | |
"polyglot_notebook": { | |
"kernelName": "fsharp" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"exception MFSharpError1 of string\n", | |
"exception MFSharpError2 of string * int" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"That's it! Defining new exception classes is a lot easier than in C#!\n", | |
"\n", | |
"## Throwing exceptions\n", | |
"\n", | |
"There are three basic ways to throw an exception\n", | |
"\n", | |
"* Using one of the built in functions, such as \"invalidArg\"\n", | |
"* Using one of the standard .NET exception classes\n", | |
"* Using your own custom exception types \n", | |
"\n", | |
"### Throwing exceptions, method 1: using one of the built in functions\n", | |
"\n", | |
"There are four useful exception keywords built into F#: \n", | |
"\n", | |
"* `failwith` throws a generic `System.Exception`\n", | |
"* `invalidArg` throws an `ArgumentException`\n", | |
"* `nullArg` throws a `NullArgumentException`\n", | |
"* `invalidOp ` throws an `InvalidOperationException`\n", | |
"\n", | |
"These four probably cover most of the exceptions you would regularly throw. Here is how they are used:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"dotnet_interactive": { | |
"language": "fsharp" | |
}, | |
"polyglot_notebook": { | |
"kernelName": "fsharp" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"// throws a generic System.Exception\n", | |
"let f x =\n", | |
" if x then \"ok\"\n", | |
" else failwith \"message\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"dotnet_interactive": { | |
"language": "fsharp" | |
}, | |
"polyglot_notebook": { | |
"kernelName": "fsharp" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"// throws an ArgumentException\n", | |
"let f x =\n", | |
" if x then \"ok\"\n", | |
" else invalidArg \"paramName\" \"message\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "Error", | |
"evalue": "(1,7): error CS1002: ; expected\r\n(1,10): error CS1525: Invalid expression term 'if'\r\n(1,10): error CS1002: ; expected\r\n(2,8): error CS1003: Syntax error, '(' expected\r\n(2,10): error CS1026: ) expected\r\n(2,15): error CS1002: ; expected\r\n(2,19): error CS1002: ; expected\r\n(2,19): error CS8641: 'else' cannot start a statement.\r\n(2,19): error CS1003: Syntax error, '(' expected\r\n(2,19): error CS1525: Invalid expression term 'else'\r\n(2,19): error CS1026: ) expected\r\n(2,19): error CS1525: Invalid expression term 'else'\r\n(2,19): error CS1002: ; expected\r\n(3,18): error CS1002: ; expected\r\n(3,30): error CS1002: ; expected", | |
"output_type": "error", | |
"traceback": [ | |
"(1,7): error CS1002: ; expected\r\n", | |
"(1,10): error CS1525: Invalid expression term 'if'\r\n", | |
"(1,10): error CS1002: ; expected\r\n", | |
"(2,8): error CS1003: Syntax error, '(' expected\r\n", | |
"(2,10): error CS1026: ) expected\r\n", | |
"(2,15): error CS1002: ; expected\r\n", | |
"(2,19): error CS1002: ; expected\r\n", | |
"(2,19): error CS8641: 'else' cannot start a statement.\r\n", | |
"(2,19): error CS1003: Syntax error, '(' expected\r\n", | |
"(2,19): error CS1525: Invalid expression term 'else'\r\n", | |
"(2,19): error CS1026: ) expected\r\n", | |
"(2,19): error CS1525: Invalid expression term 'else'\r\n", | |
"(2,19): error CS1002: ; expected\r\n", | |
"(3,18): error CS1002: ; expected\r\n", | |
"(3,30): error CS1002: ; expected" | |
] | |
} | |
], | |
"source": [ | |
"let f x =\n", | |
" if x then \"ok\"\n", | |
" else nullArg \"paramName\" \"message\"" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": ".NET (F#)", | |
"language": "F#", | |
"name": ".net-fsharp" | |
}, | |
"polyglot_notebook": { | |
"kernelInfo": { | |
"defaultKernelName": "fsharp", | |
"items": [ | |
{ | |
"aliases": [], | |
"languageName": "fsharp", | |
"name": "fsharp" | |
} | |
] | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment