[Libguestfs] [PATCH 1/6] python: use errors more specific than RuntimeError

Peter Wu peter at lekensteyn.nl
Sat Aug 16 11:28:45 UTC 2014


KeyError is documented as:
"Raised when a mapping (dictionary) key is not found in the set of
existing keys."

TypeError is documented as:
"Raised when an operation or function is applied to an object of
inappropriate type."

RuntimeError is documented as:
"Raised when an error is detected that doesn’t fall in any of the other
categories."

Let's be more specific with exceptions. The exception all inherit from
StandardError (and from Exception), but not from RuntimeError, so this
might need some code changes (if users would catch this specific error).
However, this behavior was not documented so it is perfectly fine to
change it.
---
 generator/generator.ml | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/generator/generator.ml b/generator/generator.ml
index 0218b90..c65a3bd 100755
--- a/generator/generator.ml
+++ b/generator/generator.ml
@@ -2846,7 +2846,7 @@ get_value (PyObject *v, hive_set_value *ret)
 
   obj = PyDict_GetItemString (v, \"key\");
   if (!obj) {
-    PyErr_SetString (PyExc_RuntimeError, \"no 'key' element in dictionary\");
+    PyErr_SetString (PyExc_KeyError, \"no 'key' element in dictionary\");
     return -1;
   }
 #ifdef HAVE_PYSTRING_ASSTRING
@@ -2858,14 +2858,14 @@ get_value (PyObject *v, hive_set_value *ret)
 
   obj = PyDict_GetItemString (v, \"t\");
   if (!obj) {
-    PyErr_SetString (PyExc_RuntimeError, \"no 't' element in dictionary\");
+    PyErr_SetString (PyExc_KeyError, \"no 't' element in dictionary\");
     return -1;
   }
   ret->t = PyLong_AsLong (obj);
 
   obj = PyDict_GetItemString (v, \"value\");
   if (!obj) {
-    PyErr_SetString (PyExc_RuntimeError, \"no 'value' element in dictionary\");
+    PyErr_SetString (PyExc_KeyError, \"no 'value' element in dictionary\");
     return -1;
   }
 #ifdef HAVE_PYSTRING_ASSTRING
-- 
2.0.4




More information about the Libguestfs mailing list