I am curious when job status and exceptions are used. Say I have a TextProcessor
class with getText()
, processText()
and sendText()
. All these methods perform operations on the private data of the class. I need to make sure that all operations go smoothly. I have two options: each method can return the status of the operation (0 - success,> 0 error codes) or throw an exception inside the method. It seems that exceptions are a more convenient way to control the execution of the method, because I will need to do the following when the work statuses are returned:
$result = textProcessor->getText(); if ( $result !== 0 ) { return $result; } $result = textProcessor->processText(); if ( $result !== 0 ) { return $result; } $result = textProcessor->sendText(); if ( $result !== 0 ) { return $result; }
or in this way
if ( textProcessor->getText() !== 0 && textProcessor->processText() !== 0 && textProcessor->sendText() !== 0 ) { return processingErrors::textProcessorError; }
It all seems a lot simpler with exceptions:
try { textProcessor->getText(); textProcessor->processText(); textProcessor->sendText(); } catch (textProcessorException $e) { return $e->getMessage(); }
1) So what is better to use in my situation - operation statuses or exceptions?
2) In general, when do I use job statuses (return codes) and with exceptions?
source share