Note: This tutorial assumes that you have completed the previous tutorials: Debug Modes. |
Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
Error Handling in the ECL
Description: Shows how the ecl does error handling at the simplest level, with macros and flags.Keywords: ecl error handling
Tutorial Level: BEGINNER
Next Tutorial: Assertions
Macros
ecl_errors defines a macro, LOC, is a means of picking up the current file and line number of the point at which it is processed. Example code further below.
Error Flags
A set of flags is provided through the enumeration ecl::ErrorFlag. These, when coupled with the ecl::Error (error handler) class and LOC macro provide a convenient means of quickly identifying errors produced by the ecl libraries regardless of the platform that is used.
1 ecl::Error void f(const int &i) const {
2 if ( i == 3 ) {
3 return ecl::Error(ecl::NoError);
4 } else {
5 return ecl::Error(ecl::OutOfRangeError);
6 }
7
8 int main() {
9 ecl::Error error = f(5);
10 if ( error.flag() != NoError() ) {
11 error.print(LOC);
12 }
13 // output is ~ 'my_main.cpp : 11 - Out of range error.'
14 return 0;
15 }
The ecl::Error class can be extended to customise the error messages - refer to ecl_time_lite and the ecl::TimeError class for an example.