summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFlaviu Tamas <tamasflaviu@gmail.com>2015-05-19 17:47:54 -0400
committerFlaviu Tamas <tamasflaviu@gmail.com>2015-05-19 17:47:54 -0400
commitcaa730127bbe1ba359eb4640ea52cd4e1e5cf540 (patch)
treedbc7cba68032037366ddc53589873101299e3b71
parentae0c8573f2ac1840373948b086e1f4d4c6a4180f (diff)
downloadNim-caa730127bbe1ba359eb4640ea52cd4e1e5cf540.tar.gz
Use custom exception for option unpack
-rw-r--r--lib/pure/optionals.nim11
1 files changed, 6 insertions, 5 deletions
diff --git a/lib/pure/optionals.nim b/lib/pure/optionals.nim
index 30b2505ad..6fda5996e 100644
--- a/lib/pure/optionals.nim
+++ b/lib/pure/optionals.nim
@@ -43,11 +43,11 @@
 ##
 ##   try:
 ##     assert("abc".find('c').get() == 2)  # Immediately extract the value
-##   except FieldError:  # If there is no value
+##   except UnpackError:  # If there is no value
 ##     assert false  # This will not be reached, because the value is present
 ##
 ## The ``get`` operation demonstrated above returns the underlying value, or
-## raises ``FieldError`` if there is no value. There is another option for
+## raises ``UnpackError`` if there is no value. There is another option for
 ## obtaining the value: ``unsafeGet``, but you must only use it when you are
 ## absolutely sure the value is present (e.g. after checking ``isSome``). If
 ## you do not care about the tiny overhead that ``get`` causes, you should
@@ -67,7 +67,7 @@
 ##   try:
 ##     echo result.get()
 ##     assert(false)  # This will not be reached
-##   except FieldError:  # Because an exception is raised
+##   except UnpackError:  # Because an exception is raised
 ##     discard
 
 import typetraits
@@ -78,6 +78,7 @@ type
     ## An optional type that stores its value and state separately in a boolean.
     val: T
     has: bool
+  UnpackError* = ref object of ValueError
 
 
 proc some*[T](val: T): Option[T] =
@@ -106,7 +107,7 @@ proc get*[T](self: Option[T]): T =
   ## Returns contents of the Option. If it is none, then an exception is
   ## thrown.
   if self.isNone:
-    raise newException(FieldError, "Can't obtain a value from a `none`")
+    raise UnpackError(msg : "Can't obtain a value from a `none`")
   self.val
 
 
@@ -144,7 +145,7 @@ when isMainModule:
       check some("a").isSome
 
     test "none":
-      expect FieldError:
+      expect UnpackError:
         discard none(int).get()
       check(none(int).isNone)
       check(not none(string).isSome)