changeset 15731:18f168880226

error_ids: Adding ids and documentation error_ids.m: Added ID Octave:bad-alloc and Octave:undefined-function error.txi: Added section that explains how to catch errors with ids for this I needed a working example. I chose the ID Octave:invalid-indexing. ov-struct.cc: When a structure is indexed with a field it doesn't have the error now returns the corresponding ID Octave:invalid-indexing.
author Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
date Tue, 04 Dec 2012 20:06:49 +0100
parents 84d29b282130
children 82b0ad43a939
files doc/interpreter/errors.txi libinterp/octave-value/ov-struct.cc scripts/miscellaneous/error_ids.m
diffstat 3 files changed, 49 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/doc/interpreter/errors.txi	Tue Dec 04 16:40:22 2012 -0500
+++ b/doc/interpreter/errors.txi	Tue Dec 04 20:06:49 2012 +0100
@@ -180,6 +180,39 @@
 
 @DOCSTRING(lasterr)
 
+It is also possible to assign an identification string to an error.
+If an error has such an ID the user can catch this error
+as will be shown in the next example.  To assign an ID to an error,
+simply call @code{error} with two string arguments, where the first
+is the identification string, and the second is the actual error.  Note
+that error IDs are in the format "NAMESPACE:ERROR-NAME".  The namespace
+"Octave" is used for Octave's own errors.  Any other string is available
+as a namespace for user's own errors.
+
+The next example counts indexing errors. The errors are catched using the
+field identifier of the structure returned by the function @code{lasterror}.
+
+@example
+@group
+number_of_errors = 0;
+for n = 1:100
+  try
+    @dots{}
+  catch
+    id = lasterror.identifier;
+    if (strcmp (id, "Octave:invalid-indexing"))
+      number_of_errors++;
+    endif
+  end_try_catch
+endfor
+@end group
+@end example
+
+The functions distributed with Octave can issue one of the following
+errors.
+
+@DOCSTRING(error_ids)
+
 When an error has been handled it is possible to raise it again.  This
 can be useful when an error needs to be detected, but the program should
 still abort.  This is possible using the @code{rethrow} function.  The
@@ -305,6 +338,11 @@
 
 @DOCSTRING(lastwarn)
 
+The functions distributed with Octave can issue one of the following
+warnings.
+
+@DOCSTRING(warning_ids)
+
 @node Enabling and Disabling Warnings
 @subsection Enabling and Disabling Warnings
 
@@ -334,9 +372,4 @@
 @end group
 @end example
 
-The functions distributed with Octave can issue one of the following
-warnings.
 
-@DOCSTRING(warning_ids)
-
-
--- a/libinterp/octave-value/ov-struct.cc	Tue Dec 04 16:40:22 2012 -0500
+++ b/libinterp/octave-value/ov-struct.cc	Tue Dec 04 20:06:49 2012 +0100
@@ -85,7 +85,8 @@
   else if (auto_add)
     retval = (numel () == 0) ? Cell (dim_vector (1, 1)) : Cell (dims ());
   else
-    error ("structure has no member '%s'", nm.c_str ());
+    error_with_id ("Octave:invalid-indexing", 
+                   "structure has no member '%s'", nm.c_str ());
 
   return retval;
 }
@@ -1143,7 +1144,8 @@
   retval = map.getfield (nm);
 
   if (! auto_add && retval.is_undefined ())
-    error ("structure has no member '%s'", nm.c_str ());
+    error_with_id ("Octave:invalid-indexing",
+                   "structure has no member '%s'", nm.c_str ());
 
   return retval;
 }
--- a/scripts/miscellaneous/error_ids.m	Tue Dec 04 16:40:22 2012 -0500
+++ b/scripts/miscellaneous/error_ids.m	Tue Dec 04 20:06:49 2012 +0100
@@ -36,6 +36,13 @@
 ## Indicates that a data-type was indexed incorrectly, e.g., real-value index
 ## for arrays, non-existent field of a structure.
 ##
+## @item Octave:bad-alloc
+## Indicates that memory couldn't be allocated.
+##
+## @item Octave:undefined-function
+## Indicates a call to a function that is not defined. The function may
+## exist but Octave is unable to find it in the search path.
+##
 ## @end table