summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Pedersen <david.pdrsn@gmail.com>2013-12-05 12:33:02 +0100
committerDavid Pedersen <david.pdrsn@gmail.com>2013-12-05 12:33:02 +0100
commita03869362b5706345ecbb3783bd7f6173d12b698 (patch)
tree7811143b09d0146448be0da1e89ef4aaaf4c491a
parentf1c96d5db0dae92e13be96a03118da2565ab3327 (diff)
[standard-ml/en-en] Exceptions
-rw-r--r--standard-ml.html.markdown22
1 files changed, 22 insertions, 0 deletions
diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown
index 56b6853c..547ca39b 100644
--- a/standard-ml.html.markdown
+++ b/standard-ml.html.markdown
@@ -302,6 +302,28 @@ fun count (Leaf n) = n
| count (Node (leftTree, n, rightTree)) = count leftTree + n + count rightTree
+(* Exceptions! *)
+(* Exceptions can be raised using "raise" *)
+fun raiseException msg = raise Fail msg
+
+(* This raises exception `Fail "hello from exception"` *)
+(* val _ = raiseException "hello from exception" *)
+
+(* Exceptions can be caught using "handle" *)
+val x = raiseException "hello" handle Fail msg => msg
+(* x now has the value "hello" *)
+
+(* We can pattern match in "handle" to make sure
+ a specfic exception was raised, or grab the message *)
+val y = raiseException "..." handle Fail _ => "Fail was raised"
+ | Domain => "Domain was raised"
+(* y now has the value "Fail was raised" *)
+
+(* We can define our own exceptions like this *)
+exception MyException
+exception MyExceptionWithMessage of string
+
+
(* File I/O! *)
(* Write a nice poem to a file *)
fun writePoem(filename) =