Download 1 Introduction
Transcript
10 Exception Handling Microb uses the exception handling function from C++ based on the concept of the try and catch blocks and the throw operator. When an exception occurs in a library, an object is “thrown” and the application is responsible for “catching” it. If no catch block takes care of the problem, the application ends with an error message. This situation is not desirable in a robot control context. 10.1 Error objects When an error occurs in Microb, an object representing the type of error is launched. The object also contains a message providing information on the error that occurred. The object classes are defined in the OSDL/error_handling.h file. The classes are Generic_error, Warning and Fatal_error. The Generic_error class is a basic class from which other classes inherits. The print method for these classes is used to display the message contained in the object, while the get_error_type and get_message methods return the type of error and the message, respectively. 10.2 Try and catch blocks To catch one of the error objects, the code must be placed where the error may occur in a try block (set off by brackets). Following the block, one or more catch blocks must be placed. The catch operator uses as argument the type of error which the user wishes to catch and a name for the object (optional). The block is also set off by brackets. The user may place more than one catch block after a try block; only the first catch block with a type that corresponds to that of the error (or to one of its basic types) will be executed. The following code provides a simple example of exception handling. The line contained in the try block attempts to add two matrix classes with different dimensions. The + operator will detect the problem and launch a Fatal_error object. As this object inherits from Generic_error, the catch block (Generic_error error) will catch the object. The object’s print method is called up to display the error message. Exemple 24 : #include "Vectmath/vectmath.h" #include "OSDL/error_handling.h" Microb – User’s Manual 75