Common Graphics : Image and icon file formats : Handling errors

Handling errors
If an error occurs during a load, the loadFromFileHandle:atOffset: method answers nil. Another way to detect an error is to send the hasErrorOccurred message to the format object (the instance of the CgFileFormat subclass). This method answers true if an error occurred in the last operation, or false if no error occurred. To determine what the error was, the currentError and currentErrorString messages can be sent to the format object. The currentError method answers an integer error code. The currentErrorString method answers a String describing the error. The following table lists the error codes and corresponding error strings.
Table 26. CgFileFormat error codes and error strings
The following example adds error handling to the previous example of loading a PCX file:
| format image |
format := CgPCXFileFormat new.
image := format
loadFromFile: 'my-image.pcx'.
format hasErrorOccurred
ifTrue: [self error: format currentErrorString]
ifFalse: [^image]