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
Error code
Error string
1
No errors
2
Error opening file
3
Invalid file handle
4
Invalid offset
5
Invalid array of ByteArrays
6
Invalid array of integer offsets into ByteArrays
7
Seek error
8
Write error
9
Read error
10
Read-only object encountered (attempt to unload into ByteArray marked as read-only)
11
Memory allocation error
12
File format not supported
13
File is not of specified format
14
Compression type not supported
15
Unsupported aspect of the specified format encountered
16
Invalid extra info
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]
Last modified date: 05/13/2020