📄 except.html
字号:
<HTML><HEAD><TITLE>Newmat09 - exceptions</TITLE></HEAD><BODY><H2>Exceptions</H2><A HREF="cleanup.html"> next</A> - <A HREF="cleanup.html"> skip</A> - <A HREF="refer.html"> up</A> - <A HREF="index.html"> start</A><P>Here is the class structure for exceptions:<PRE>Exception Logic_error ProgramException miscellaneous matrix error IndexException index out of bounds VectorException unable to convert matrix to vector NotSquareException matrix is not square (invert, solve) SubMatrixDimensionException out of bounds index of submatrix IncompatibleDimensionsException (multiply, add etc) NotDefinedException operation not defined (eg <) CannotBuildException copying a matrix where copy is undefined InternalException probably an error in newmat Runtime_error NPDException matrix not positive definite (Cholesky) ConvergenceException no convergence (e-values, non-linear, sort) SingularException matrix is singular (invert, solve) SolutionException no convergence in solution routine OverflowException floating point overflow Bad_alloc out of space (new fails)</PRE><P>I have attempted to mimic the exception class structure in the C++ standardlibrary, by defining the Logic_error and Runtime_error classes.<P>If there is no catch statement or exceptions are disabled then my <TT>Terminate()</TT>function in <TT>myexcept.h</TT> is called. This prints out an error message,the dimensions and types of the matrices involved, the name of the routinedetecting theexception, and any other information set by the <A HREF="error.html">Tracer</A>class. Alsosee the section on <A HREF="error.html">error messages</A> for additional noteson the messages generated by the exceptions.<P>You can also print this information by printing <TT>Exception::what()</TT>.<P>See the file <TT>test_exc.cpp</TT> as an example of catching an exception andprinting the error message.<P>The 08 version of newmat defined a member function<TT>void SetAction(int)</TT> to help customise the action when anexception is called. This has been deleted in the 09 version. Now includean instruction such as <TT>cout << Exception::what() << endl;</TT> inthe <TT>Catch</TT> or <TT>CatchAll</TT> block to determine the action.<P>The library includes the alternatives of using the inbuilt exceptions provided bya compiler, simulating exceptions, or disabling exceptions.See <A HREF="custom.html">customising</A> for selecting the correct exceptionoption. <P>The rest of this section describes my partial simulation of exceptions forcompilers which do not support C++ exceptions. I use Carlos Vidal'sarticle in the September 1992 <I>C Users Journal</I> as a starting point.<P>Newmat does a partial clean up of memory following throwing an exception- see the next section. However, the present version will leave a littleheap memory unrecovered under some circumstances. I would not expectthis to be a major problem, but it is something that needs to be sortedout.<P>The functions/macros I define are Try, Throw, Catch, CatchAll andCatchAndThrow. Try, Throw, Catch and CatchAll correspond to try, throw,catch and catch(...) in the C++ standard. A list of Catch clauses mustbe terminated by either CatchAll or CatchAndThrow but not both. Throwtakes an Exception as an argument or takes no argument (for passing onan exception). I do not have a version of Throw for specifying whichexceptions a function might throw. Catch takes an exception class nameas an argument; CatchAll and CatchAndThrow don't have any arguments.Try, Catch and CatchAll must be followed by blocks enclosed in curlybrackets.<P>I have added another macro ReThrow to mean a rethrow, Throw().This was necessary to enable the package to be compatible with both myexception package and C++ exceptions.<P>If you want to throw an exception, use a statement like<PRE> Throw(Exception("Error message\n"));</PRE>It is important to have the exception declaration in the Throw statement, ratherthan as a separate statement.<P>All exception classes must be derived from the class, Exception, defined innewmat and can contain only static variables. See the examples in newmatif you want to define additional exceptions.<P><A HREF="cleanup.html"> next</A> - <A HREF="cleanup.html"> skip</A> - <A HREF="refer.html"> up</A> - <A HREF="index.html"> start</A><P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -