class ErrorCode
Defined at line 84 of file ../../third_party/icu/default/source/common/unicode/errorcode.h
Wrapper class for UErrorCode, with conversion operators for direct use
in ICU C and C++ APIs.
Intended to be used as a base class, where a subclass overrides
the handleFailure() function so that it throws an exception,
does an assert(), logs an error, etc.
This is not an abstract base class. This class can be used and instantiated
by itself, although it will be more useful when subclassed.
Features:
- The constructor initializes the internal UErrorCode to U_ZERO_ERROR,
removing one common source of errors.
- Same use in C APIs taking a UErrorCode * (pointer)
and C++ taking UErrorCode
&
(reference) via conversion operators.
- Possible automatic checking for success when it goes out of scope.
Note: For automatic checking for success in the destructor, a subclass
must implement such logic in its own destructor because the base class
destructor cannot call a subclass function (like handleFailure()).
The ErrorCode base class destructor does nothing.
Note also: While it is possible for a destructor to throw an exception,
it is generally unsafe to do so. This means that in a subclass the destructor
and the handleFailure() function may need to take different actions.
Sample code:
ICU 4.2
Code
class IcuErrorCode: public icu::ErrorCode {
public:
virtual ~IcuErrorCode() { // should be defined in .cpp as "key function"
// Safe because our handleFailure() does not throw exceptions.
if(isFailure()) { handleFailure(); }
}
protected:
virtual void handleFailure() const {
log_failure(u_errorName(errorCode));
exit(errorCode);
}
};
IcuErrorCode error_code;
UConverter *cnv = ucnv_open("Shift-JIS", error_code);
length = ucnv_fromUChars(dest, capacity, src, length, error_code);
ucnv_close(cnv);
// IcuErrorCode destructor checks for success.
Protected Members
UErrorCode errorCode
Public Methods
void ErrorCode ()
Default constructor. Initializes its UErrorCode to U_ZERO_ERROR.
ICU 4.2
Defined at line 90 of file ../../third_party/icu/default/source/common/unicode/errorcode.h
UErrorCode & operator UErrorCode & ()
Conversion operator, returns a reference.
ICU 4.2
Defined at line 94 of file ../../third_party/icu/default/source/common/unicode/errorcode.h
UErrorCode * operator UErrorCode * ()
Conversion operator, returns a pointer.
ICU 4.2
Defined at line 96 of file ../../third_party/icu/default/source/common/unicode/errorcode.h
UBool isSuccess ()
Tests for U_SUCCESS().
ICU 4.2
Defined at line 98 of file ../../third_party/icu/default/source/common/unicode/errorcode.h
UBool isFailure ()
Tests for U_FAILURE().
ICU 4.2
Defined at line 100 of file ../../third_party/icu/default/source/common/unicode/errorcode.h
UErrorCode get ()
Returns the UErrorCode value.
ICU 4.2
Defined at line 102 of file ../../third_party/icu/default/source/common/unicode/errorcode.h
void set (UErrorCode value)
Sets the UErrorCode value.
ICU 4.2
Defined at line 104 of file ../../third_party/icu/default/source/common/unicode/errorcode.h
void ~ErrorCode ()
Destructor, does nothing. See class documentation for details.
ICU 4.2
UErrorCode reset ()
Returns the UErrorCode value and resets it to U_ZERO_ERROR.
ICU 4.2
void assertSuccess ()
Asserts isSuccess().
In other words, this method checks for a failure code,
and the base class handles it like this:
ICU 4.4
Code
if(isFailure()) { handleFailure(); }
const char * errorName ()
Return a string for the UErrorCode value.
The string will be the same as the name of the error code constant
in the UErrorCode enum.
ICU 4.4
Protected Methods
void handleFailure ()
Called by assertSuccess() if isFailure() is true.
A subclass should override this function to deal with a failure code:
Throw an exception, log an error, terminate the program, or similar.
ICU 4.2
Defined at line 137 of file ../../third_party/icu/default/source/common/unicode/errorcode.h