Sophie

Sophie

distrib > Mageia > 7 > aarch64 > by-pkgid > 641ebb3060c35990cc021d8f7aaf9aca > files > 229

octave-doc-5.1.0-7.1.mga7.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Exception and Error Handling in Oct-Files (GNU Octave (version 5.1.0))</title>

<meta name="description" content="Exception and Error Handling in Oct-Files (GNU Octave (version 5.1.0))">
<meta name="keywords" content="Exception and Error Handling in Oct-Files (GNU Octave (version 5.1.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Oct_002dFiles.html#Oct_002dFiles" rel="up" title="Oct-Files">
<link href="Documentation-and-Testing-of-Oct_002dFiles.html#Documentation-and-Testing-of-Oct_002dFiles" rel="next" title="Documentation and Testing of Oct-Files">
<link href="Input-Parameter-Checking-in-Oct_002dFiles.html#Input-Parameter-Checking-in-Oct_002dFiles" rel="prev" title="Input Parameter Checking in Oct-Files">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">


</head>

<body lang="en">
<a name="Exception-and-Error-Handling-in-Oct_002dFiles"></a>
<div class="header">
<p>
Next: <a href="Documentation-and-Testing-of-Oct_002dFiles.html#Documentation-and-Testing-of-Oct_002dFiles" accesskey="n" rel="next">Documentation and Testing of Oct-Files</a>, Previous: <a href="Input-Parameter-Checking-in-Oct_002dFiles.html#Input-Parameter-Checking-in-Oct_002dFiles" accesskey="p" rel="prev">Input Parameter Checking in Oct-Files</a>, Up: <a href="Oct_002dFiles.html#Oct_002dFiles" accesskey="u" rel="up">Oct-Files</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Exception-and-Error-Handling-in-Oct_002dFiles-1"></a>
<h4 class="subsection">A.1.12 Exception and Error Handling in Oct-Files</h4>

<p>Another important feature of Octave is its ability to react to the user typing
<tt class="key">Control-C</tt> during extended calculations.  This ability is based on the C++
exception handler, where memory allocated by the C++ new/delete methods is
automatically released when the exception is treated.  When writing an oct-file
which may run for a long time the programmer must periodically use the macro
<code><span class="nolinebreak">OCTAVE_QUIT</span></code><!-- /@w -->, in order to allow Octave to check and possibly respond
to a user typing <tt class="key">Control-C</tt>.  For example:
</p>
<div class="example">
<pre class="example">for (octave_idx_type i = 0; i &lt; a.nelem (); i++)
  {
    OCTAVE_QUIT;
    b.elem (i) = 2. * a.elem (i);
  }
</pre></div>

<p>The presence of the <code><span class="nolinebreak">OCTAVE_QUIT</span></code><!-- /@w --> macro in the inner loop allows
Octave to detect and acknowledge a <tt class="key">Control-C</tt> key sequence.  Without this
macro, the user must either wait for the oct-file function to return before the
interrupt is processed, or the user must press <tt class="key">Control-C</tt> three times
which will force Octave to exit completely.
</p>
<p>The <code><span class="nolinebreak">OCTAVE_QUIT</span></code><!-- /@w --> macro does impose a very small performance penalty;
For loops that are known to be small it may not make sense to include
<code><span class="nolinebreak">OCTAVE_QUIT</span></code><!-- /@w -->.
</p>
<p>When creating an oct-file that uses an external library, the function might
spend a significant portion of its time in the external library.  It is not
generally possible to use the <code><span class="nolinebreak">OCTAVE_QUIT</span></code><!-- /@w --> macro in this case.  The
alternative code in this case is
</p>
<div class="example">
<pre class="example">BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
&hellip;  some code that calls a &quot;foreign&quot; function &hellip;
END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
</pre></div>

<p>The disadvantage of this is that if the foreign code allocates any memory
internally, then this memory might be lost during an interrupt, without being
deallocated.  Therefore, ideally Octave itself should allocate any memory that
is needed by the foreign code, with either the <code>fortran_vec</code>
method or the <code><span class="nolinebreak">OCTAVE_LOCAL_BUFFER</span></code><!-- /@w --> macro.
</p>
<p>The Octave <code>unwind_protect</code> mechanism (<a href="The-unwind_005fprotect-Statement.html#The-unwind_005fprotect-Statement">The unwind_protect Statement</a>)
can also be used in oct-files.  In conjunction with the exception handling of
Octave, it ensures that certain recovery code is always run even if an
exception occurs.  An example of the use of this mechanism is
</p>
<div class="example">
<pre class="verbatim">#include &lt;octave/oct.h&gt;
#include &lt;octave/unwind-prot.h&gt;

void
my_err_handler (const char *fmt, ...)
{
  // Do nothing!!
}

void
my_err_with_id_handler (const char *id, const char *fmt, ...)
{
  // Do nothing!!
}

DEFUN_DLD (unwinddemo, args, nargout, &quot;Unwind Demo&quot;)
{
  if (args.length () &lt; 2)
    print_usage ();

  NDArray a = args(0).array_value ();
  NDArray b = args(1).array_value ();

  // Declare unwind_protect frame which lasts as long as
  // the variable frame has scope.
  octave::unwind_protect frame;
  frame.add_fcn (set_liboctave_warning_handler,
                 current_liboctave_warning_handler);

  frame.add_fcn (set_liboctave_warning_with_id_handler,
                 current_liboctave_warning_with_id_handler);

  set_liboctave_warning_handler (my_err_handler);
  set_liboctave_warning_with_id_handler (my_err_with_id_handler);

  return octave_value (quotient (a, b));
}
</pre></div>

<p>As can be seen in the example:
</p>
<div class="example">
<pre class="example">unwinddemo (1, 0)
&rArr; Inf
1 / 0
&rArr; warning: division by zero
   Inf
</pre></div>

<p>The warning for division by zero (and in fact all warnings) are disabled in the
<code>unwinddemo</code> function.
</p>
<hr>
<div class="header">
<p>
Next: <a href="Documentation-and-Testing-of-Oct_002dFiles.html#Documentation-and-Testing-of-Oct_002dFiles" accesskey="n" rel="next">Documentation and Testing of Oct-Files</a>, Previous: <a href="Input-Parameter-Checking-in-Oct_002dFiles.html#Input-Parameter-Checking-in-Oct_002dFiles" accesskey="p" rel="prev">Input Parameter Checking in Oct-Files</a>, Up: <a href="Oct_002dFiles.html#Oct_002dFiles" accesskey="u" rel="up">Oct-Files</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>